3

In my app, I model Lists of items. In MainActivity, I see ListView containing Lists. I can click on each list to see its items (DisplayListActivity). On DisplayListActivity, I have button in the ActionBar to display the list properties. 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 appears, when I select List id=2 on the MainActivity, and then I select the properties button on the DisplayListActivity. Once I am done with the EditListPropertiesActivity, i click '<' (back) on the ActionBar: enter image description here.

I return to the DisplayListActivity, but instead of going back to the list id=2, I see the list with id=1 (which is default).

How to pass the ListId back form EditListPropertiesActivity to the DisplayListActivity?

  1. startActivityForResult and return the id - would work, but I see it ugly
  2. use the SharedPreferences and store sth like lastVisitedList - ugly
  3. store the lastVisitedList information in the db - even uglier

What is the usual solution for that problem?

Code snippets below.

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);
    ...

Manifest

<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>
vikin9
  • 497
  • 4
  • 16

3 Answers3

1

try this to finish an activity

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

and this for getting the result in the previous activity

@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

nadavfima
  • 3,012
  • 1
  • 11
  • 9
  • I am finishing the activity by hitting the '<' back button on the ActionBar. Is there any built-in function like onHitBackButton? Would be onStop() appropriate? – vikin9 Sep 22 '14 at 19:20
  • I checked onStop(), onPause(), and onDestroy()-unfortunately it doesn't work. The code onActivityResult is not called at all. – vikin9 Sep 22 '14 at 19:35
  • yup, there's **[this one.](http://stackoverflow.com/a/18337567/3989632)** Though you need to make sure you don't call the `super.onBackPressed();` method like the example answer does. – nadavfima Sep 23 '14 at 13:40
  • Oh, and when you start the activity you need to use `startActivityForResult` instead of `startActivity`. **[Here's an example from Google](http://developer.android.com/training/basics/intents/result.html)** on how to do this, and **[here's one](http://stackoverflow.com/a/10407371/3989632)** from stackoverflow. – nadavfima Sep 23 '14 at 13:45
  • Yes, I used the `startActivityForResult`. I will give a try with the `onBackPressed` method. – vikin9 Sep 24 '14 at 10:46
  • The returning of the value works, but `onBackPressed` is called when I press the OS back-button. I am looking for a method that is called when I press the [back button on the ActionBar](http://developer.android.com/images/training/implementing-navigation-up.png) – vikin9 Sep 24 '14 at 19:35
  • Then [**this is what you need**](http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp): `@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: doSomethingWithStuff(); return true; } return super.onOptionsItemSelected(item); }` – nadavfima Sep 25 '14 at 09:19
0

try to call finish(); that you should call on destroy of the current activity and display the previous activity.

0

I have found the solution that works. I post it here, because it is cleaner (in my opinion) and different than the other proposed. I set the launch mode of the second activity to singleTask in the 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 the stated problem, I am open for discussion :)

vikin9
  • 497
  • 4
  • 16