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();
    }
有帮助吗?

解决方案

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);

其他提示

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.

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();

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(); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top