3

From my MainActivity (Launcher Activity) I press a button to start my GameActivity. I have it so when I hit the Back button in Game Activity, I don't return to my MainActivity and instead return to my home screen. Now when I resume my app it goes to MainActivity instead of returning to GameActivity despite being shown.


Goal

Main Activity -> Game Activity -> Home -> Game Activity

Current Result

Main Activity -> Game Activity -> Home -> Home Activity


A couple things..

  • It works perfectly when I navigate with my home and menu buttons.
  • I have no launchMode in my Manifest

So let us see what I have done!


Main Activity Button Click

 Button startButton = findViewById(R.id.buttonStart);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, GameActivity.class);
            //stops back button to return to add player screen
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);

        }
    });

Manifest Simplified

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainPage.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".GamePage.GameActivity"
        android:label="@string/title_activity_game"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait" />
</application>
C. Skjerdal
  • 2,750
  • 3
  • 25
  • 50
  • How do you resume your app ? (the steps) – Cao Minh Vu Mar 14 '18 at 02:03
  • I click the menu button, it appears, and I tap on it with my finger. Aside from that I have nothing. I assumed it should just go to the correct activity being the other stacks were wiped. – C. Skjerdal Mar 14 '18 at 02:15
  • please refer here: https://stackoverflow.com/questions/8631095/android-preventing-going-back-to-the-previous-activity/26492794 – Rohit Sharma Mar 14 '18 at 03:15
  • Those are solutions for stopping returning to the previous activity, they do not solve the issue or resuming the application with my method. Those answers has also been suggested below in the comments and I've tried the various different things commented in your link and no change in result. – C. Skjerdal Mar 14 '18 at 03:24
  • 1
    I think you have scrolled down too quickly the link and might have missed Asok's answer on "android:launchMode" and android documentation ("singleTask" or "singleInstance") But, anyways happy that it worked for you. :) – Rohit Sharma Mar 14 '18 at 04:26

3 Answers3

1

In that case, just call finish() right after launching you GameActivity.

Button startButton = findViewById(R.id.buttonStart);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, GameActivity.class);
            startActivity(intent);
            finish();
        }
    });

Edit: Update launchMode for your GameActivity also:

<activity android:name=".GameActivity" android:launchMode="singleInstance">
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
0

when you tap on the back button of device the method onBackPress is called, you can override him to redirect user according your necessity, which mean you can change the action for the Back Button, Code:

//When Back Button on click, it will come to this method.
@Override
public void onBackPressed() {
    //Do anything you want here, 
}
I am a Student
  • 1,570
  • 4
  • 21
  • 36
0

It doesn't seem like this is possible to do using the conventional back-stack mechanism. You can't return to the home screen using the back button without popping off all the Activities in your back-stack first (hence destroying them, if you have no memory leaks).

Then, when you click the menu button, a screenshot of your last displayed Activity should be shown, but since all of your Activities are destroyed your launcher Activity is loaded.

One way you can handle re-entering the app at the point of your choosing is by implementing a navigation Activity (in many cases this is a splash screen) as the launcher Activity. In this Activity, you would determine (using persistent storage i.e. SharedPreferences) which Activity/back-stack state the app should be set to.

For this example, you could store a boolean with key isInGame in SharedPreferences when you enter GameActivity. Then in the navigation activity, you can retrieve the value and launch GameActivity if true. Obviously, for handling game/app state you will have to store a lot more, but this demonstrates simple navigation.

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
urgentx
  • 3,832
  • 2
  • 19
  • 30
  • That makes much more sense, I couldn't figure out the logic in this behavior. This also works well since I planned to do a splash screen in the next day or two. I will get to work on this and see where I get. – C. Skjerdal Mar 14 '18 at 03:29
  • I almost voted this comment correct but an above comment found an easier solution, thanks for the help. I would say this is also a great way to solve this issue. – C. Skjerdal Mar 14 '18 at 04:00