比如一个ACTIVTY当显示他的时候,他要弹出一个登陆,来输入用户口令
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . main );
... loadSettings ();
if ( strSessionString == null )
{ login ();
}
... }
但是比如配置变化了,这个ACT被摧毁重新创建那么又要谈一次
方法有好多:
You can have a static field to store this kind of state. Or put it to the resource Bundle and restore from there on onCreate(Bundle savedInstanceState).
再深入考虑,你可能要跨越这个APP的好多ACT共享一些信息
更好的办法:
each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.
The way to do this is to create your own subclass of android.app.Application , and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):
class
MyApp
extends
Application
{
private
String
myState
;
public
String
getState
(){
return
myState
;
}
public
void
setState
(
String
s
){
myState
=
s
;
}
}
class
Blah
extends
Activity
{
@Override
public
void
onCreate
(
Bundle
b
){
...
MyApp
appState
=
((
MyApp
)
getApplicationContext
());
String
state
=
appState
.
getState
();
...
}
}