android 用context共享变量 、代替静态变量static、 activity之间传递变量

来源:互联网 发布:金融互助平台源码下载 编辑:程序博客网 时间:2024/05/22 16:01


Android中在不同Activity中传递变量,通常使用Intent中Bundle添加变量的操作方法。


  view plaincopy to clipboardprint?


  Intent intent = new Intent();


  intent.setClass(A.this, B.class);


  Bundle bundle = new Bundle();


  bundle.putString("Info", "Information");


  intent.putExtras(bundle);


  startActivity(intent);


  Intent


  intent = new Intent();


  intent.setClass(A.this, B.class);


  Bundle bundle = new Bundle();


  bundle.putString("Info", "Information");


  intent.putExtras(bundle);


  startActivity(intent);


  不过在多个Activity中经常使用同一变量时,使用Bundle则比较麻烦,每次调用Activity都需要设置一次。


  如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。


  新建一个类,继承自Application


  view plaincopy to clipboardprint?


  class MyApp extends Application {


  private String myState;


  public String getState() {


  return myState;


  }


  public void setState(String s) {


  myState = s;


  }


  }


  class MyApp extends


  Application {


  private String myState;


  public String getState() {


  return myState;


  }


  public void setState(String s) {


  myState = s;


  }


  }


  在AndroidManifest.xml的application加个name属性就可以了,如下面所示:


  view plaincopy to clipboardprint?


  


  < p> 


  android:name=".MyApp" android:icon="@drawable/icon"


  android:label="@string/app_name">


  使用时:


  view plaincopy to clipboardprint?


  class Blah extends Activity {


@Override


  public void onCreate(Bundle b){


  ...


  MyApp appState = ((MyApp)getApplicationContext());


  String state = appState.getState();


  ...


  }


  }


  class Blah extends


  Activity {


  @Override


  public void onCreate(Bundle b){


  ...


  MyApp appState = ((MyApp)getApplicationContext());


  String state = appState.getState();


  ...


  }


  }


  英文引用:http://stackoverflow.com/questions/708012/android-how-to-declare- global-variables


  The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.


  --如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。


  As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.


  --每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。


  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 andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):


  --方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是 android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。





0 0
原创粉丝点击