1

Up Navigation on Action Bar closes app in Android. Followed android developer exactly. I added .getSupportActionBar,.setDisplayHomeAsUpEnabled, inflated my menu, put my case in onCreateOptionsMenu but is still doesn't work.

public class AddressList extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address_list);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ListView listview = (ListView) findViewById(R.id.listView);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_address_list, menu);
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

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

Here is my manifest file to show that I've added parentactivity name and the metadata.

  <activity
   android:name=".SilentGeofence"
        android:label="@string/title_activity_silent_geofence"
        android:parentActivityName=".AddressList" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".AddressList" />

    </activity>
anu
  • 703
  • 1
  • 10
  • 19
  • Do you receive any error msg? – Joaquin Iurchuk Mar 13 '15 at 02:31
  • 03-12 19:35:00.529 16221-16221/com.example.anusha.app W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection @joaquin – anu Mar 13 '15 at 02:35
  • Well, your Log is absolutely incomplete. But it seems to be that the problem is not on the code you posted. – Joaquin Iurchuk Mar 13 '15 at 12:31
  • I know it's a bit old, but, well, what struck me first is that you show us code for `AddressList(Activity)`, and then you show in Manifest it's a PARENT for `SilentGeofence`. Shouldn't the code snippet be in `SilentGeofence` actually? – Antek Jul 06 '17 at 07:17

3 Answers3

0

I have had the same issue previously. Try using the following code snippet. Credit goes to the link in the snippet:

case android.R.id.home:

            //solution for the up navigation problem:
            //http://stackoverflow.com/questions/13632480/android-build-a-notification-taskstackbuilder-addparentstack-not-working
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is NOT part of this app's task, so create a new task
                // when navigating up, with a synthesized back stack.
                TaskStackBuilder.create(this)
                        // Add all of this activity's parents to the back stack
                        .addNextIntentWithParentStack(upIntent)
                                // Navigate up to the closest parent
                        .startActivities();
            } else {
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                NavUtils.navigateUpTo(this, upIntent);
                Log.v("recreate activity", "shouldUpRecreateTask = false");
            }
Wheater
  • 58
  • 5
0

in android:value try to use full package name of parent Activity

    <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.AddressList" />
Zia
  • 2,365
  • 2
  • 17
  • 14
0

If anyone still needs an answer here is how I was able to do it.

//First set Parent activity in your xml as shown above


<activity
       android:name=".ChildActivity"
            android:label="@string/title"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />

        </activity>

//Now you can simply get the parent Intent and use startActivity
@Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                Intent upIntent=NavUtils.getParentActivityIntent(this);
//You might need to add some Launch Parameters like
                upIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(upIntent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

You can find more Information about these launch parameters here: Launch Parameter Info

Hardi
  • 3
  • 4