0

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();
    }
John R
  • 2,078
  • 8
  • 35
  • 58
  • 2
    why do you want to exit app. read this http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan Feb 04 '14 at 09:22
  • 1
    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 Feb 04 '14 at 09:25
  • 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 Feb 04 '14 at 09:29
  • @2Dee I know but why the above code is not finishing all activities? – John R Feb 04 '14 at 09:32

4 Answers4

1

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);
Yugesh
  • 4,030
  • 9
  • 57
  • 97
0

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.

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

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();
Ankit
  • 483
  • 3
  • 8
0

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(); 
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • Whenever I start my app for first time and press home button app has to resume and start from that point on next time. But its start from start. After that whenever I run my app and press home its resume from that point. – John R Feb 05 '14 at 04:46