0

I have a main activity and a child activity. I am using up button in child activity to return to main activity. In my activity I have some animation that I don't want to start it again when I return to it form child activity. So here is home action in child activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = NavUtils.getParentActivityIntent(this);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

And here is manifest:

 <activity
      android:name=".ChildActivity"
      android:label="@string/app_name"
      android:parentActivityName=".MainActivity" >

      <!-- Parent activity meta-data to support 4.0 and lower -->
      <meta-data
          android:name="android.support.PARENT_ACTIVITY"
          android:value=".MainActivity" />
</activity>

When I run it on LG nexus 5 with android 4.4.4 it works fine but in Xperia arc with android 4.0.4 it starts the animation each time I return to main activity. What's the solution?

Update:

I can achieve what I want using one of these ways:

1- Just calling finish() when up is pressed.

2- Using Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP when adding flag to intent.

But I want to know what causes this difference in behavior in android 4.4.4 and 4.0.4.

Thanks in advance!

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • please add your comments to your question, this way when people read it can easily understand the situation:-) – mmlooloo Oct 04 '14 at 10:09

1 Answers1

0

Per Providing Up Navigation, you can use NavUtils.navigateUpFromSameTask(this); rather than constructing an Intent yourself as this will handle the flags correctly to return to the parent activity.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • It causes both devices to start animation when I return to main activity. – Misagh Emamverdi Oct 04 '14 at 17:37
  • 1
    Right, Up and Back (what is done by `finish()`) are different operations: back is simply closing the current screen and going to where the user came from while Up involves creating a semantic, consistent parent state. If you don't have `singleTop` on your parent activity's manifest entry, then you'll see it totally recreating the parent activity and the animation you see. – ianhanniballake Oct 04 '14 at 18:22