译自:http://www.developerphil.com/dont-store-data-in-the-application-object/
The Application object:
// access modifiers omitted for brevity classMyApplication extends Application { String name; String getName() { returnname; } voidsetName(String name) { this.name = name; } }The first activity, where we store the name of the user in the application object:
// access modifiers omitted for brevity classWhatIsYourNameActivity extends Activity { voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.writing); // Just assume that in the real app we would really ask it! MyApplication app = (MyApplication) getApplication(); app.setName("Developer Phil"); startActivity(newIntent(this, GreetLoudlyActivity.class)); } }
The second activity, where we shout the name of the user:
// access modifiers omitted for brevity classGreetLoudlyActivity extends Activity { TextView textview; voidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reading); textview = (TextView) findViewById(R.id.message); } voidonResume() { super.onResume(); MyApplication app = (MyApplication) getApplication(); textview.setText("HELLO " + app.getName().toUpperCase()); } }
# find the process id adb shell ps # then find the line with the package name of your app # Mac/Unix: save some time by using grep: adb shell ps | grep your.app.package # The result should look like: # USER PID PPID VSIZE RSS WCHAN PC NAME # u0_a198 21997 160 827940 22064 ffffffff 00000000 S your.app.package # Kill the app by PID adb shell kill -9 21997 # the app is now killed
注:经过自己的测试,确实出现如作者所描述的情况