0
votes

In my app I have menu page that contains 4 buttons History, Types, Benefits and Exit. After Splash Screen menu page opens. If I start History, Type or Benefit activity. I am not finish menu activity because if I finish this then on press on navigating up icon in action bar app close. When I am in any of the 3 activities and press device back button then I came back to menu activity. And if then I press exit or back button from device. EndSplash starts and app finish thats I want. But after one second app restart. How to close app on exit and back button?

I also have navigating tabs below action bar. Minimum API level 8.

In menu activity-

exit = (Button) findViewById(R.id.exit);
        exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                i = new Intent(MainActivity.this, EndSplash.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                startActivity(i);
                finish();
            }
        });
@Override 
    public void onBackPressed() {
        super.onBackPressed();
        i = new Intent(MainActivity.this, EndSplash.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i);
        finish();
    } 

And in History,Types and Benefits Activity-

public void onBackPressed()  
    {    
    startActivity(new Intent(this, MainActivity.class));  
        finish();
    }
4
why do you want to exit app. read this stackoverflow.com/questions/2033914/… - Raghunandan
As Raghunandan asked:) I'm curious as well as normally you would not "exit" the application as next time user needs to wait longer for application to open. - Lukasz 'Severiaan' Grela
You have an EndSplashScreen, showing when the user leaves the app ? To me, that is terrible UX, completely out of touch with how Android usually works... - 2Dee
@2Dee I know but why the above code is not finishing all activities? - John R

4 Answers

1
votes

Try this one to Exit the application:-

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
0
votes

You should make use a BroadcastReceiver on button press you should finish all the activities registered to it. you should register all the activities on its oncreate(). There are lot of implementations out there for broadcast receiver.

0
votes

To finish all your activities you can make a Utils.java class and add this method on it:

public static List<Context> listContext = new ArrayList<Context>();

public static void finishActivities() 
{

    System.out.println("inside finishActivities:: ");

    for (Context context : Utils.listContext) {
        if(context!=null)
         ((Activity) context).finish();
    }
    Utils.listContext.clear();
}

and add your activity context to list like:

Utils.listContext.add(this);

and call this method from your onBackPressed() Activity like:

Utils.finishActivities();
0
votes

you can use this code:

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
PackageManager pm1 = context.getPackageManager();
List<android.content.pm.ResolveInfo> activities = pm1.queryIntentActivities(homeIntent, 0);
String className = activities.get(0).activityInfo.taskAffinity;
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startMain.setPackage(className);
context.startActivity(startMain);
((Activity) context).finish();