0

I have many activities and have to traverse through all activities from navigation drawer. I want to quit app from home screen whether there are many activities which have not been finished.For this purpose i used NavUtils as we can move to parent activity from "NavUtils.navigateUpFromSameTask(MyClass.this)" method. for example i have two activities here splashScreen.class and ManinActivity.class i startactivity from splashScreen activity by finishing the splashScreen & splashscreen is parentclass of MainActivity & by quitting app from mainactivity the app is finished in some mobiles but in some mobiles the app goes to parent activity's onCreate method so called splashScreen.I don't understand why this is happening.Help me if u know better procedure or if i'm doing something wrong here is my code of quitting app from MainActivity:

NavUtils.navigateUpFromSameTask(MainActivity.this);

and i start MainActivity from splashScreen like this:

public void StartMainActivity()
{
    Intent mainActivity = new Intent(splashScreen.this,MainActivity.class);
    startActivity(mainActivity);
    finish();
}

here is manifest code:

<activity
        android:name=".MainActivity"
        android:label="StoneAge"
        android:parentActivityName="com.experlabs.brandappy.splashScreen">
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.experlabs.brandappy.splashScreen" />
    </activity>
Hamza Khalil
  • 481
  • 1
  • 7
  • 18
  • check the docs http://developer.android.com/training/implementing-navigation/ancestral.html – Raghunandan Jan 22 '14 at 06:42
  • @Raghunandan i have read this i just don't know why it starts splashScreen in a few mobile if that has been finished – Hamza Khalil Jan 22 '14 at 06:46
  • or is there any better procedure to quit app from a specific activity? – Hamza Khalil Jan 22 '14 at 06:47
  • crete a baseActivity by extending Activity in that override onPause and call `finish()`. Make all your activities extend from BaseActivity. or clear backstack and call `finish()`. But when you click back button it is supposed to take you back to the previous activity. That is what it is meant to do. So re-think your design – Raghunandan Jan 22 '14 at 06:51
  • @Raghunandan how'll i code to run finish all activities in onPause of BaseActivity? and what'll be the procedure run onPause method of BaseActivity then? – Hamza Khalil Jan 22 '14 at 07:10
  • read this http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon. then decide if you really want to quit the application – Raghunandan Jan 22 '14 at 07:11

2 Answers2

1

When you decide to quit the app you can do it by doing 2 things

  1. set a specific resultCode to the activity that means QUIT_APP and finish the activity

  2. override function onActivityResult in all your activities (or inherit froma singel one) and for resultCode QUIT_APP repeat step a.

Basically it will cascade all your activities

edit:

also in your code call startActivityForResult instead of startActivity. See this for more info https://stackoverflow.com/a/1124988/1393632

Community
  • 1
  • 1
asaf gitai
  • 400
  • 2
  • 10
0

I have done this.You can finish your app by just making an activity like DashBoard which does only calls finish() method in Oncreate method.& where you want to quit your app you can enter the Flag_Activity_Clear_Top and Flag_Activity_New_task in intent like this:

Intent intent = new Intent(MainActivity.this, DashBoardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

and where u want to call any previous activity by skipping inbetween activities u can add that activity in parent activity mentioned in xml and when u have to navigate just enter the code:

NavUtils.navigateUpFromSameTask(this);
Hamza Khalil
  • 481
  • 1
  • 7
  • 18