is it possible to fetch the object value without calling a new activity?
I know that stuff of calling the new activity and pass the object value from one activity to another by using putExtra and getExtra function. but i want 开发者_开发百科to pass the value without calling and start the new activity. Is it possible ? If yes then let me know how i can do it ?
You can also use the Application class for declaring global variables
class Globalclass extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class TempActivity extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
UPDATED :
Checkout this nice Tutorial about how to do that.
Application Class Using as Global
for this you can use static variable
or SharedPreferences
or if you heavy data then you can use SQlite
.
You can take the help of database like SQLite or you may go for Constant class concept where you can make a public static variable and store your data in one activity and access in other activity.Hope this will help you.
There're a lot of ways to pass a value to an activity:
- You can use an
Intent
withFLAG_ACTIVITY_REORDER_TO_FRONT
. In this caseonNewIntent()
method will be called for already started activity. - You can use static fields or static methods to pass new data to your activity. But it's not a good method really because sometimes application is terminated even if it's foreground and all static data is lost.
- You can send new data to an activity using broadcast messages. In this case the activity must register a
BroadcastReceiver
.
I think it's not very difficult to make up a few more ways to pass arguments.
You may want to use handler's handleMessage() and pass the object in a message .
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
}
};
You can then call handler.handleMessage(msg), you can assign any object to msg.onj
精彩评论