flagg327 flagg327 - 4 months ago 20
Android Question

Force to launch SplashActivity onRestart another activity

I have an SplashActivity that create an ArrayList of custom CommerceObjects. This List will be used in the rest of the app, in diferent activities and fragments. The problem is that sometimes, when the app is stoped and then restarted, the objects List appear as not initialized. The solution is to check if the ArrayList is not null and in the case of being null force the SplashActivity launch again and remake the ArrayList. I tried to do this in the onRestart method in the rest of activities but is not working at all.

For example, this is the way that I'm checking in MainActivity if the ArrayList (created in SplashActivity) is null.

public class MainActivity extends AppCompatActivity {
...

@Override
protected void onRestart() {
// If the full list of commerces is null or is empty, launch the SplashActivity.
// Here check if the ArrayList of CommerceObjects is null
if (SplashActivity._commerces == null || SplashActivity._commerces.size() == 0) {
Intent mIntent = new Intent(MainActivity.this, SplashActivity.class);
startActivity(mIntent);
this.finish();
}

super.onRestart();
}

...
}


So, the array list to check is "_commerces". It's decalred as public static in SplashActivity. I need to check if is not null no mather what fragment or activity is currently in the front of the stack.

What I'm missing?

Answer

UPDATE

I recommend you to use onStart(). onRestart() is not called if App process is killed by Android OS. https://developer.android.com/reference/android/app/Activity.html

ORIGINAL

The static variables will be initialized by Android OS.
see: static variable null when returning to the app

So I recommend you to avoid using static variables. Make your Application class and Hold the CommerceObjects in your Application instance.

Following codes explains.

Make your Application class:

public class App extends Application {

    private CommerceObjects mCommerces;

    public void setCommerces(CommerceObjects commerces) {
        mCommerces = commerces;
    }

    public CommerceObjects getCommerces() {
        return mCommerces;
    }

    public static App get(Context context) {
        return (App) context.getApplicationContext();
    }
}

Set name of application in your AndroidManifest.xml:

<application
        ...
        android:name=".App">

    ...

</application>

Initialize commerces in your SplashActivity:

public class SplashActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        initializeCommerces();
    }

    private void initializeCommerces() {
        //do initialize tasks
        ...
        CommerceObjects commerces = ...;

        //set CommerceObjects to App
        App.get(this).setCommerces(commerces);

        //start other Activity. ex) MainActivity
    }
}

Use commerces in anther Activity:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //use CommerceObjects
        CommerceObjects commerces = App.get(this).getCommerces();
        ...
    }
}