Try not to save static data in Application

4937 단어
During the development process, since Application is a globally unified object, we can design it as a singleton pattern and save some static data in it, but is this really good? Do a test here: 1) Save a name information in a custom Application
public class MApplication extends Application{

    public String name;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
2) In the first Activity, we save the name information to the Application
public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MApplication app = (MApplication) getApplication();
                app.setName("this is a test demo!!!");
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
    }

}
3) In the second Activity we get the data saved by the first activity:
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        MApplication app = (MApplication) getApplication();
        Log.i("tag", app.getName());
    }

}
4) Test First, we start the App, click the button, the name information we specified will be saved in the Application, and then go back to the name information saved by the first Activity in the SecondActivity, but at this time we press the home button, a few hours later , the Android system kills the app in order to reclaim memory. At this time, if the user reopens the app. The Android system creates a new MyApplication instance and restores SecondActivity, and then SecondActivity gets the user name from the new MyApplication instance, which is empty, and finally results in NullPointerException. In the above example, the reason for the app crash is that the Application object is brand new, so the value in the name variable is null, so when we get the name again, it may cause a NullPointerException.
The crux of the problem is: the application object will not stay in memory all the time, it will be killed. Contrary to popular belief, the app doesn't actually start over. The Android system will create a new Application object, and then start the activity the last time the user left it to give the illusion that the app has never been killed.
There are several solutions here:

  • Pass data directly to Activity via intent.
  • Persist data to disk in several officially recommended ways.
  • Always do a non-null check on the value of a variable when using data.
  • 좋은 웹페이지 즐겨찾기