Search code examples
androidandroid-manifestandroid-toolbar

Navigating back to Parent activity with toolbar


so to break it down.

I have two activites: A main activity and a subactivity.

When I am in the subactivity, I want to press the return home button on the toolbar and return back home.

I tried putting android.support.PARENT_ACTIVITY in manifest, but I still couldn't get it to work. Maybe I'm not seeing something obvious here. Hope you guys can help.

Here is my subactivity.

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.final_create_cardview_1_image);

        //Toolbar
        myToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_pic);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    private void setSupportActionBar(Toolbar myToolbar)
    {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if(id == android.R.id.home)
        {
            NavUtils.navigateUpFromSameTask(this);
        }

        return super.onOptionsItemSelected(item);
    }

Now here is my manifest. The first activity being the main activity. And the second activity being the subactivity.

<activity
            android:name=".MyActivity"
            android:label="Promenade"
            android:parentActivityName=".MyActivity">
            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".CardFinal.imageActivity">
            </meta-data>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


  </activity>
        <activity android:name=".CardFinal.imageActivity"
            android:label="PickImage">
        </activity>

I'm not quite confident if I'm doing the manifest correctly, the other solutions on stackoverflow are very confusing. If I can have a straight forward answer to this, that would be awesome. Thanks!


Solution

  • @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if(id == android.R.id.home)
        {
            //NavUtils.navigateUpFromSameTask(this);
              finish();
        }
    
        return super.onOptionsItemSelected(item);
    }