これを行うには、現在のアクティビティから次のように起動する前に、共有設定に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
読んでください:SharedPreferences
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;
}
}