Geek
Geek

Reputation: 1530

Finishing main activity cause android application to restart

I'm facing strange issue in an android application. Whenever I finish activity or even force close application it got restarted.

Here is what application flow:

Application opens with Splash screen which is an activity and then go to Main Activity where content shown in a fragment. Now when I finish main activity on back press then application got restarted instead of closing.

I'm starting MainActivity like this

Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);

and in MainActivity i'm finishing it like this

MainActivity.this.finish();

and exiting application like this

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);

How to resolve this issue?

Thanks

Upvotes: 2

Views: 2774

Answers (6)

Naimesh Chokhawala
Naimesh Chokhawala

Reputation: 1

@MukeshJha problem is that you are not finishing your first activity i.e. SplashActivity so it was remain in stack in stop state when you started MainActivity and then you are finishing your MainActivity so that leads to restart previous SplashActivity from stop state.

To avoid again restart SplashActivity you need to finish it whenever you are starting new activity.

solution is to start MainActivity like this way,

Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish(); // this will cause to finish SplashActivity.

Upvotes: 0

Prashant
Prashant

Reputation: 1613

After you start MainActivity call finish() , that will solve your problem.

In SplashActivity.java

Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();

You don't need to do anything else in MainActivity

Upvotes: 1

David Wasser
David Wasser

Reputation: 95626

Do not do this:

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);

There is no need to kill the process. Android will do this when and if it wants to. This is the reason your app is getting restarted. Android thinks it crashed, and is restarting it for the user.

Upvotes: 1

kulbhushan jain
kulbhushan jain

Reputation: 71

Buddy your code is

Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
MainActivity.this.finish();

Here what's wrong with your code is that you are first opening MainActivity and once it's opened you are closing it in very next line.

Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish(); // or write this as SplashActivity.this.finish();

This piece of code will finish your current activity that is SplashActivity and MainActivity will remain active. Good Luck.

Upvotes: 0

Vij
Vij

Reputation: 445

can you post some code?? i post this code may be this will help you.

inside your Mainactivity.

 @Override
   public void onBackPressed() {
    super.onBackPressed();
    finish();
  }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            finish();
            break;

    }
    return (super.onOptionsItemSelected(item));
}

Upvotes: 0

Kunal Yadav
Kunal Yadav

Reputation: 139

You should start MainActivity with flags as

Intent launchNextActivity;
launchNextActivity = new Intent(B.class, A.class);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);                  
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(launchNextActivity);

Now in the MainActivity where you want to implement onBackPressed to close the app, you may do something like this,

private Boolean exit = false;
@Override
    public void onBackPressed() {
        if (exit) {
            finish(); // finish activity
        } else {
            Toast.makeText(this, "Press Back again to Exit.",
                    Toast.LENGTH_SHORT).show();
            exit = true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    exit = false;
                }
            }, 3 * 1000);

        }

}

Now when you press back button it will give toast to "Press back gain to exit", and if user presses back button within 3 seconds the app will exit

Upvotes: 1

Related Questions