Domanda

I have an application that calls an activity several times from different activitys. So, im trying to implement the "back button" in the action bar for this activity. For doing this im using:

 switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    default:        
         return super.onOptionsItemSelected(item);
    }

and:

<meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="view.TweetsIndividuoActivity" />

The problem now, is that i cannt set a parent activity to my android manifest, cause, i don't know who is the parent of this activity.

What is the solution ?

Thanks

È stato utile?

Soluzione

It's easier than you think.

switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    default:        
         return super.onOptionsItemSelected(item);
}

Method finish() will destroy your activity and show the one that started it. That's what you want if I understood you right.

Your current solution is meant for cases when you want go back to the same parent every time e.g. Gmail app does it. When you open email from notification and then press actionbar back button it will not navigate back to HOME screen but it will show you Gmail inbox.

Altri suggerimenti

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
        return  true;
    }
    return super.onOptionsItemSelected(item);

}

You will always go back to the activity from which you have launched the new activity.

No need to use the code below.

<meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="view.TweetsIndividuoActivity" />

I'm a newbie to Android too, but I solved this problem by calling the 2nd activity using "startActivityForResult(intent,1)" instead of "startActivity(intent)". I think this makes it a parent/child relationship instead of a sibling activity...?

I didn't need to use onOptionsItemSelected() or finish().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top