Returning to the previous activity using the ActionBar navigation

In my application I am modeling lists of items. In MainActivity, I see a ListView containing lists. I can click on each list to see its elements (DisplayListActivity). In DisplayListActivity, I have a button in the ActionBar to display the properties of the list. This launches the third activity (EditListPropertiesActivity).

              Main
              | ^
              V |
      DisplayListActivity (listId is passed with Intent, default=1)
              | ^
              V |
   EditListPropertiesActivity (listId is passed with Intent, default=1)

      

The problem comes when I select List id = 2 in MainActivity and then I select the properties button in DisplayListActivity. As soon as I finished with EditListPropertiesActivity, I press '<' (back) in the ActionBar: enter image description here.

I go back to DisplayListActivity, but instead of going back to list id = 2, I see a list with id = 1 (default).

How do I pass the returned form ListId of EditListPropertiesActivity to DisplayListActivity?

  • startActivityForResult and return id - will work, but I see it ugly
  • use SharedPreferences and save sth as lastVisitedList - ugly
  • save lastVisitedList info to db - even uglier

What's the usual solution for this problem?

Below are the code snippets.

MainActivity

public class MainActivity extends Activity {
...
listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    MetaList mlist = (MetaList) listView.getItemAtPosition(i);

                    final Intent intent;
                    intent = new Intent(getApplicationContext(), DisplayListActivity.class);
                    intent.putExtra(META_LIST_SELECTED, mlist.getId());
                    startActivity(intent);
                }
            }
    );
...

      

DisplayListActivity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list);

    intent = getIntent();
    metaListId = intent.getLongExtra(MainActivity.META_LIST_SELECTED, 1); //read the data or take 1 as default
    ...
    //start the third activity
    final Intent intent;
    intent = new Intent(getApplicationContext(), EditListPropertiesActivity.class);
    intent.putExtra(META_LIST_SELECTED, metaListId);
    startActivity(intent);
    ....

      

EditListPropertiesActivity

public class EditListPropertiesActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_list_parameters);

    getActionBar().setDisplayHomeAsUpEnabled(true);  //this enables the '<' (back) button 

    intent = getIntent();
    metaListId = intent.getLongExtra(DisplayMetaListActivity.META_LIST_SELECTED, 1);
    ...

      

manifesto

<application>
    <activity
            android:name=".MainActivity">
    </activity>
    <activity
            android:name="com.example.tutorial1.DisplayListActivity"
            android:parentActivityName="com.example.tutorial1.MainActivity" >
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.tutorial1.MainActivity" />
    </activity>
    <activity
            android:name="com.example.tutorial1.EditListPropertiesActivity"
            android:parentActivityName="com.example.tutorial1.DisplayListActivity" >
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.tutorial1.DisplayListActivity" />
    </activity>
</application>

      

+3


source to share


3 answers


I found a solution that works. I am posting it here because it is cleaner (in my opinion) and different from the one suggested. I have set the run mode of the second activity in singleTask in my manifest file:android:launchMode="singleTask"

    ...
    <activity
        android:name="com.example.tutorial1.DisplayListActivity"
        android:label="Meta List"
        android:parentActivityName="com.example.tutorial1.MainActivity"
        android:launchMode="singleTask">
        <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.tutorial1.MainActivity" />
    </activity>
    ...

      



If there is a better solution for this problem, I am open to discussion :)

0


source


try this to complete the operation

Intent intent = new Intent();
intent.putExtra(EXTRA, value);
setResult(RESULT_OK, output);
finish();

      

and this is to get the result in the previous action



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(data.getExtras().containsKey(EXTRA)){

       // do stuff with data from finished activity

       String bla = data.getStringExtra(EXTRA)

    }
}

      

EDIT: read comments! need to use startActivityForResult

+1


source


try calling finish (); that you should cause the destruction of the current activity and display the previous activity.

0


source







All Articles