在android studio中重新打开应用程序后如何打开最近的avtivity

enxuqcxy  于 2021-08-25  发布在  Java
关注(0)|答案(3)|浏览(378)

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

<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>
juud5qan

juud5qan1#

你用 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 ));
    }

请参阅更多SharedReferences

代码

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();
}

}

zpqajqem

zpqajqem2#

创建默认活动作为启动器活动`公共类默认扩展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中,在保存首选项时使用:public class 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中,保存首选项时使用:public class 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();
}

}`

bvk5enib

bvk5enib3#

您可以尝试使用android清单中定义的启动模式。尝试单一任务。让我知道这是否有帮助。

相关问题