0

I use toolbar in activity like this:

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And this code in AppCombatActivity:

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

In Manifest I defined parent activity.

It works. But everytime I press the button 'onCreate' is called on parent activity. But thats not what I want. Whenn I press the back-button on the device it goes back to previous activity and just calls onResume. That is what it also should do when I press the back button in the toolbar.

Any Ideas? Thanks!

  • If the parent activity has not been created before or it is closed with `finish();` of course it will call `onCreate()`. – KlevisGjN Mar 08 '17 at 08:37

3 Answers3

1

In your manifest file add

launchmode="singleTop"

in the parent activity's declaration. Check out LorenzCK's answer: https://stackoverflow.com/a/15933890/5987223

Community
  • 1
  • 1
drishit96
  • 327
  • 1
  • 2
  • 18
0

Add this for your requirement...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //Start activity which you want... to call onCreateMethod
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}
Chirag.T
  • 746
  • 1
  • 6
  • 18
0

This code works for me:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            **onBackPressed();**
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}