0

This is my second activity,

@Override
public void onBackPressed() {

    super.onBackPressed();
    try {
            object.put_stats(getApplicationContext());
        }
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

    this.finish();
    overridePendingTransition(R.anim.slideouttoright, R.anim.slideinfromleft);

}

But when I'm pressing the back button I'm getting directed to the home screen. This is where I call the 2nd activity from,

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivityForResult(intent, 1);
            overridePendingTransition(R.anim.slideinfromright, R.anim.slideouttoleft);

I want to go to the last activity from the 2nd activity upon pressing back button. I don't want to start a new activity by starting an intent.

ree1991
  • 197
  • 1
  • 1
  • 20
  • 1
    What happens when you comment out `super.onBackPressed();` ? See also http://stackoverflow.com/questions/6413700/android-proper-way-to-use-onbackpressed – Tigger Aug 24 '15 at 12:24
  • when i comment out super.onBackpressed it takes me to home screen. and with the super.onBackpressed call, it takes me to the menu screen. – ree1991 Aug 24 '15 at 12:30
  • Can you also include the parts of your manifest where you declare the activities, and any other relevant code (intent for the first task, onDestroy, onPause, onResume, etc. if they do anything that's related with the lifecycle) – Sebastian Aug 24 '15 at 12:48

3 Answers3

0
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

setFlags will replace the flag that you added first. This is probably not what you want.

Your intent has then only Intent.FLAG_ACTIVITY_CLEAR_TOP as flags, which means:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

So most probably your previous Activity is just being removed from the stack, so there is nowhere to go back to. Use addFlags in both cases.

Sebastian
  • 1,076
  • 9
  • 24
  • same result. it's taking me back to menu screen. – ree1991 Aug 24 '15 at 12:35
  • But what happens if you include no flag at all? what I point out is that the flag might force the removal of the first activity from the stack, try it without any flag. – Sebastian Aug 24 '15 at 12:49
0

I will suggest you use Fragment for simplicity.
With the Fragment you should not have to worry about that with the help of addToBackStack method. If not too late or laborious migrate your activities to fragments.

Want2bExpert
  • 527
  • 4
  • 11
0

Can you add your manifest?

Are you sure you have set up your second activity to be child of the first one ?

Check also backwards compatibility for Android 4.0 and lower

    <activity
    android:name="com.example.myfirstapp.SecondActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
    </activity>

You have Up Navigation and parent activities explained here:

http://developer.android.com/intl/es/training/implementing-navigation/ancestral.html#SpecifyParent

Javier Delgado
  • 2,343
  • 2
  • 17
  • 31