0

I have an activity with fragments in Android, and if I minimize the app and after a while the system kills the process, later when I run the app it crashes, because it tries to resume from the second activity (the one with fragments), even though the process has been killed and it is NOT a launcher activity. The reason it crashes is because in the fragments some view use variables from a Singleton class, and when the process is killed all those variables become null. So I tried with a check in the parent activity that hosts the fragments like this

public class SecondActivity extends FragmentActivity {

    FragmentTabsAdapter tabsAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Crash", "Second Activity");
        if (Commons.INST.someVariableFromTheSingleton == null) {
            Intent i = new Intent(this, SplashScreen.class);
                        // Splash screen is the launcher activity
            startActivity(i);
        } else {
            setContentView(R.layout.activity_second);
            tabsAdapter = new FragmentTabsAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(tabsAdapter);
        }
    }

}

The problem is that when it tries to restore the activity even though I explicitly stated to start the launcher activity, it still proceeds with this second activity lifecycle methods.... How do I stop it once it starts the intent?

Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81

4 Answers4

0

Look into using SharedPreferences with an Application class for your singleton. From the android documents,

You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Alex
  • 1,100
  • 1
  • 9
  • 23
0

Try to call finish() after startActivity(i)

TommyNecessary
  • 1,377
  • 1
  • 13
  • 19
0

Does the SecondActivity activity have intent filter MAIN in AndroidManifest.xml as shown below

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>

if it does try to remove this filter.

Yury Zaitsev
  • 126
  • 4
  • no, the other activities have this and even though only the SplashScreen has LAUNCHER, this problem occurs – Sartheris Stormhammer Jan 14 '14 at 14:44
  • than try to add "android.intent.action.MAIN" to SplashScreen itent filter. System will start SplashScreen acrivity instead. I dealt with such issue a long time ago, and if I remember correctly I had fixed it in this way. – Yury Zaitsev Jan 14 '14 at 14:57
0

In onCreate() you want to do this:

        Intent i = new Intent(this, SplashScreen.class);
                    // Splash screen is the launcher activity
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return;

This will only work if SplashScreen is still at the root of the activity stack (ie: it hasn't been finished). If you are currently calling finish() in SplashScreen when it launches the next Activity, you'll need to remove that so that you always have SplashScreen as the root of the Activity stack. To prevent the user from pressing BACK and going back into SplashScreen you should set a boolean flag in SplashScreen when it has launched the next Activity, and in SplashScreen.onResume() you should check this boolean flag and call finish() if the flag is set (this will then "exit" your application).

David Wasser
  • 93,459
  • 16
  • 209
  • 274