如何在安卓工作室重新打开应用程序后打开最近的活动



如何在android studio中重新打开应用程序后打开最近的activity例如,我们创建一个图书应用程序然后在重新打开应用程序后,打开我们所在的的最后一页

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo4">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


使用SharedPreferences

例如:

为最后一个活动创建一个字符串

String lastActivity="";

存储上次活动

protected void onDestroy() {
super.onDestroy();
lastActivity = "MainActivity";
// store activity Name
final SharedPreferences prefs =  this.getSharedPreferences("PREFERENCE_ACTIVITY",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("activity", lastActivity);
editor.apply();
}

做出决定:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String myActivity = prefs.getString("activity", "defValue");

if (myActivity.equals(lastActivity)){
startActivity(new Intent(this , FirstAcitvity.class ));
}else {
startActivity(new Intent(this , SecondActivity.class ));
}

参考更多SharedPreferences

代码

public class MainActivity extends AppCompatActivity {
String lastActivity = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one , two , three , four;
one = findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastActivity = "one";
startActivity(new Intent(getApplicationContext() , MainActivity2.class).putExtra("PWD" , "one"));
saveData();
}
});
two = findViewById(R.id.two);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastActivity = "two";
startActivity(new Intent(getApplicationContext() , MainActivity3.class).putExtra("PWD" , "two"));
saveData();
}
});
three = findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastActivity = "three";
startActivity(new Intent(getApplicationContext() , MainActivity4.class).putExtra("PWD" , "three"));
saveData();
}
});
four = findViewById(R.id.four);
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastActivity = "four";
startActivity(new Intent(getApplicationContext() , MainActivity5.class).putExtra("PWD" , "four"));
saveData();
}
});

}
private void saveData() {
String storeMine = lastActivity;
Log.e("PWD" , storeMine);
// store activity Name
final SharedPreferences prefs =  this.getSharedPreferences("PREFERENCE_ACTIVITY",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("activity", storeMine);
editor.apply();
}
private void loadData() {
SharedPreferences prefs = getSharedPreferences("PREFERENCE_ACTIVITY", MODE_PRIVATE);
String myActivity = prefs.getString("activity", "");
Log.e("myActivity" , myActivity);
switch (myActivity){
case "one":{
startActivity(new Intent(getApplicationContext() , MainActivity2.class).putExtra("PWD" , "one"));
break;
}
case "two":{
startActivity(new Intent(getApplicationContext() , MainActivity3.class).putExtra("PWD" , "two"));
break;
}
case "three":{
startActivity(new Intent(getApplicationContext() , MainActivity4.class).putExtra("PWD" , "three"));
break;
}
case "four":{
startActivity(new Intent(getApplicationContext() , MainActivity5.class).putExtra("PWD" , "four"));
break;
}

}
}
@Override
protected void onStart() {
super.onStart();
loadData();
}

}

创建一个默认活动作为启动器活动`公共类Default扩展了AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_default);
SharedPreferences mPrefs = getSharedPreferences("IDvalue",0);

String str = mPrefs.getString("activity", "1");
if (str.equals("1")) {
Intent intent = new Intent(Default.this,MainActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(),"Hello 1",Toast.LENGTH_SHORT).show();
} else if (str.equals("2")){
Intent intent = new Intent(Default.this,MainActivity2.class);
startActivity(intent);
}
}
public void clickD(View view)
{
Intent intent = new Intent(Default.this,MainActivity.class);
startActivity(intent);
}

}`

//在MainActivity中,保存首选项时使用:`公共类MainActivity扩展了AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences mPrefs = getSharedPreferences("IDvalue", 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("activity","1");
editor.apply();
}
public void click(View view)
{
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}

}`

//在MainActivity2中,保存首选项时使用:`公共类MainActivity2扩展了AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
SharedPreferences mPrefs = getSharedPreferences("IDvalue", 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("activity","2");
editor.apply();
}

}`

您可以尝试使用Android清单中定义的启动模式。尝试单个任务。如果这有帮助,请告诉我。

最新更新