1

I am totally new in Android and learning,and i noticed OnBackPressed takes you to the previous layout in the game, now i added this code which closes the app at OnBackPressed

@Override
    public void onBackPressed() {
        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(homeIntent);
        super.onBackPressed();
    }

My problem is when you start the game again it takes you back to the previous layout not the main layout. Take for example you have 4 activities, when i start the game it takes me to activity 3 how can i avoid this?

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon1"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        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=".StartGame"
        android:label="@string/app_name"
        android:screenOrientation="portrait"/>

    <activity
        android:name=".MathQuestions"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        />

        <activity android:name=".HighScores"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name=".HowToPlay" >

    </activity>
</application>

Madona wambua
  • 1,408
  • 1
  • 21
  • 37

2 Answers2

1

You're not really closing you app. With that code, you're simply minimizing your app (mimicing the HOME button behavior). I believe, if you call finish()after the startActivity(homeIntent) call, it will work as you want it to.

Mateus Brandao
  • 900
  • 5
  • 9
  • wrong. finish() will end the current activity, and go pop it off the backstack, returning to last activity. The application will not minimize until all activities are popped off the backstack. – Lucas Crawford Sep 02 '15 at 20:20
  • Right, I see the flag now and modified my above answer Mia. Mateus is right, the CLEAR_TOP should clear the backstack, and finish() should end your activity. remove the super.onBackPressed() and try my answer – Lucas Crawford Sep 02 '15 at 20:39
0

modify flags:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);  

and call finish() instead of super.onBackPressed();

I did some reading and found this:

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

From documentation for CLEAR_TOP

EDIT: Another solution.

Well this isn't working for you, let's do it this way:

Create a custom class that extends application, you don't need this most of the time but will help here. Call it MyApp.java

public class MyApp extends Application {
    private HashSet<Activity> mActivities;

@Override
public void onCreate(){
    super.onCreate();
    mActivities = new HashSet<Activity>();
}

public void addActivity(Activity activity){
    if(!mActivities.contains(activity))
        mActivities.add(activity);
}

public void removeActivity(Activity activity){
    if(mActivities.contains(activity))
        mActivities.remove(activity);
}
public void close(){
    for(Activity activity : mActivities){
        activity.finish();
    }
}

And add this to your android manifest in the application tag (keep rest the same):

<application
    android:name=".MyApp"
    ...
    ...

Now, in every activity call the following:

@Override
public void onStart(){
    super.onStart();
    ((MyApp) getApplication()).addActivity(this);
}

So now every activity is stored in your own HashSet can be gracefully finished when you need it to be.

Instead of calling the intent, you would now just do:

@Override
public void onBackPressed(){
    ((MyApp) getApplication()).close();
}

All activities are finished, resources released and when you come back to the app it will be at your Home Activity. Now, you may/may not need the removeActivity call for cases where an activity is destroyed or stopped, but this solution should work. Like I said, it's a little more work but would solve your problem.

Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25