2017-06-02 68 views
0

我在我的應用程序,每activity.I有一個圖像按鈕轉到主屏幕需要到主屏幕時圖像按鈕pressed.I正在使用的代碼當一個操作欄按鈕被點擊

btnLocation.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Intent intent=new Intent(SpecificCategoryActivity.this,AreaSelectionActivity.class); 

       //startActivity(intent); 
       Intent homeIntent = new Intent(SpecificCategoryActivity.this, AreaSelectionActivity.class); 
       homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(homeIntent); 

      } 
     }); 

但是,當我點擊圖像按鈕後從主屏幕導航時,它應該從它所呼叫的地方進入同一頁面。

例如,我有Activity1,Activity2,Activity3,Activity4等... Activity1是主頁,我將選擇國家和去Activity2,然後到Activity3等,但如果我點擊位置按鈕從Activity4應該進入主頁,在改變國家後必須返回到Activity4。還有一件事是,如果country已經被選中,應用程序將從Activity2啓動。

我的活動1碼是

btnStart.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      if(!autoComplete.getText().toString().isEmpty()) { 

       if (isSelectedText) { 


        Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class); 
        startActivity(intent); 


       } 
       else{ 
        editor.clear(); 
        editor.commit(); // commit changes 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CountrySelectionActivity.this); 

        alertDialogBuilder.setMessage("Please specify the correct country name"); 

        AlertDialog alertDialog = alertDialogBuilder.create(); 
        alertDialog.show(); 

       } 
      } 


     } 
    }); 

在解決問題請幫助。 在此先感謝。

+0

不要清除任務(FLAG_ACTIVITY_CLEAR_TOP),並完成活動1進行一次(不啓動前一個)。現在應顯示前一個活動。我認爲有一個更好的解決方案,但至少它應該工作 – Eselfar

回答

1
I will give some pseudo code here 
    In Activity4, locationbutton onclick listener will have the below code 
    locationbutton .setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

     //save a variable to indicate that activity4 is reached 
    savePreferences("**pagenavigated**", "4", context); //need to write //savePreferences 

    Intent intent = new Intent(activity4.this, homepage.class); 
         startActivity(intent); 

     }); 
    //We can write this method commonly so all activity can access this 
    public static void savePreferences(String key, String value, Context context) { 

      SharedPreferences SharedPreferences = PreferenceManager 
        .getDefaultSharedPreferences(context); 
      SharedPreferences.Editor editor = SharedPreferences.edit(); 
      editor.putString(key, value); 
      editor.commit(); 
     } 
    public static String loadPreferences(Context context, String key) { 
      SharedPreferences SharedPreferences = PreferenceManager 
        .getDefaultSharedPreferences(context); 
      String userDetails = SharedPreferences.getString(key, ""); 
      return userDetails; 

     } 
public static void removePreference(String key, Context context) { 

     SharedPreferences SharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     SharedPreferences.Editor editor = SharedPreferences.edit(); 
     editor.remove(key); 
     editor.commit(); 

    } 

    public static void removeAllPreferences(Context context) { 

     SharedPreferences SharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     SharedPreferences.Editor editor = SharedPreferences.edit(); 
     editor.clear(); 
     editor.commit(); 

    } 
//In Activity 1, check for sharedpreference variable value and if it is 4, it //should get navigate to 4th screen . To make this happen , after checking //the value, we need to remove the shared value as well 
//Activity 1 
//In **country dropdown change listener**, check for the value of //**pagenavigated** and if it is 4, start the activity to go for activity 4. //Before that remove the value by using removeAllPreferences method 
//So changes will be in onItemSelected 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
String navigatedPage = loadPreferences(context,"pagenavigated"); 
if (navigatedPage.equalsIgnoreCase("4") { 
removeAllPreferences(this); //Remove the preference and go to activity 4) 
Intent intent = new Intent(homepage.this, activity4.class); 
         startActivity(intent); 
} 



Hope that helps!!! 
0

在這裏您可以使用startActivityForResult(Intent intent,int resultCode);

e.g

Intent intent = new Intent(CountrySelectionActivity.this, CategoryListActvity.class); 
    intent.putExtra("Select_Location", true); 
    startActivityForResult(intent,007); 
在你的第一個活動檢查是否有意向數據,如果你得到「 Select_Location」然後選擇城市,通話結束()後旗真

現在;

例如

在活動的onCreate方法
Intent i=getIntent(); //**i** will be null if Activity is called from another Activity 

if(i!=null) 
    boolean flag=i.getBooleanExtra("Select_Location", false); 
    //now you have flag which indicates that whether this intent is from 4th activity or not. 

讓用戶選擇城市,並在您的onClick方法只是檢查我們的標誌是真還是假。

如果它是真的,那麼只需使用intent並附加所有必需的信息並調用finish。

e.g

Intent resultIntent = new Intent(); 
resultIntent.putExtra("city_id", <city id>); 
... 
setResult(Activity.RESULT_OK, resultIntent); 
finish(); 

現在需要重寫調用的方法onActivityResult(在活動4)

例如

@Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      if (resultCode == RESULT_OK && requestCode == 007 && data != null){ 
     // take out all the parameters from data(which is an object of Intent). 

      } 
    }