0

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?

flagg327
  • 967
  • 1
  • 10
  • 21

1 Answers1

1

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();
        ...
    }
}
Community
  • 1
  • 1
nshmura
  • 5,940
  • 3
  • 27
  • 46
  • Tank you for your time. This is a good aproach to the problem. But I really need to force SplashActivity relaunch. SplashActivity does other things (for example: check server status and data version to verify if there is another version of the data and download it, to remake the ArrayList of CommerceObjects). – flagg327 Aug 05 '16 at 15:33
  • Mmm, What is your problem? Can't SplashActivity launch in onRestart? – nshmura Aug 05 '16 at 15:39
  • No, the app just resume and when she needs to use the ArrayList to do something with it, crush throwing the classic java.lang.NullPointerException Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference that appears when an ArrayList has not being initialized. – flagg327 Aug 05 '16 at 15:45
  • The static variables is initialized by Android OS. So NullPointerException will occur. I can't understand you problem.. – nshmura Aug 05 '16 at 15:59
  • The Problem: "Launch SplashActivity when the app is resumed or relauched if the List _commerces is null". Just that. – flagg327 Aug 05 '16 at 16:04