demo_Ashif
demo_Ashif

Reputation: 314

Avoiding double instance of activity in android

I've done a simple content based application. App always launch with splash screen. After 3 sec it goes to MainActivity from SpalshActivity. But am facing a problem. If i press back button when app showing splash screen and again launch the app from device app list then app start normally but then I have to press back icon twice to quit the app. Because app has another instance of MainActivity from previous launch.

How can I avoid this double instance ?

public class SplashActivity extends Activity {

private static int SPLASH_TIME_OUT = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

    new Handler().postDelayed(new Runnable() {


        @Override
        public void run() {

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

            finish();
        }
    }, SPLASH_TIME_OUT);
}

 }

Upvotes: 1

Views: 2407

Answers (4)

Ozgur-K
Ozgur-K

Reputation: 11

This method can cause to open twice:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

I solved my problem by deleting this.

Upvotes: 0

Arnaud
Arnaud

Reputation: 417

let me give you my way of avoiding this error :

add this before your " super.onCreate(null);" in your onCreate method

if (!isTaskRoot()
                && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                && getIntent().getAction() != null
                && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }

super.onCreate(null);

It will finish the splashactivity whenever it is called while it wasnt the taskroot.

Second option is to get rid of the postdelayed, which is the function that messes the process.

Upvotes: 0

Jared Kells
Jared Kells

Reputation: 7062

Let me summarise the symptoms first

  • You launch the app.
  • Press back while the splash screen is displayed
  • Re-Launch the app which shows the splash screen followed by MainActivity
  • Press back which shows another MainActivity
  • Press back again which exits the app

The reason is that your postDelayed hander still runs even though you press back. Your phone is correctly starting SplashActivity but another MainActivity is being launched from the old SplashActivity.

You need to remove your postDelayed callback when you go into the background. Keep a reference to that Handler and call removeCallbacksAndMessages. I would normally start postDelayed in onResume and remove it in onPause

Alternatively you can launch MainActivity with the FLAG_ACTIVITY_CLEAR_TOP flag. That flag indicates that if MainActivity already exists in the back stack it should be brought to the front and activities above it should be closed. You can also add FLAG_ACTIVITY_SINGLE_TOP if you want it to re-use the same activity instance instead of creating a new one.

Upvotes: 1

Groco
Groco

Reputation: 1329

In your manifest.xml, add for the MainActivity:

android:launchMode="singleTop"

When you launch your activity:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

Upvotes: 2

Related Questions