您可以通过在从当前活动启动之前Shared Preferences
存储 "type","id"
在共享首选项中来执行此操作:WelcomeActivity
例如,我开始WelcomeActivity
点击按钮FirstActivity
:
public class FirstActivity extends Activity {
SharedPreferences myPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button.setOnClickListener(new OnClickListener() {
void onClick() {
//Create
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("type", type);
prefsEditor.putString("scheduleId", scheduleId);
prefsEditor.commit();
//start WelcomeActivity here
Intent wakeIntent = new Intent(Intent.ACTION_MAIN);
wakeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//welcome is launcher of the target app
wakeIntent.setClass(getApplicationContext(), WelcomeActivity.class);
wakeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(wakeIntent);
}
});
}
}
并在WelcomeActivity
ActivitySharedPreferences
中将其 onCreate
读onResume
为:
public class FirstActivity extends Activity {
SharedPreferences myPrefs;
public static boolean status=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this will read when first time start
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String strtype = myPrefs.getString("type", "nothing");
String strscheduleId = myPrefs.getString("scheduleId", "0");
status=true;
}
@Override
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
if(status!=true){
// this will read when first time start
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String strtype = myPrefs.getString("type", "nothing");
String strscheduleId = myPrefs.getString("scheduleId", "0");
}
}
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
// reset counter here
status=false;
}
}