Android 如何返回到以前的活动
我对android上活动的生命周期有问题。我正试图从第二个活动回到第一个活动。事实上,我的代码正在这样做,而不是它应该做的方式。我在左上角有一个“后退按钮”,当我点击它时,它会通过MainActivity的onDestroy()和onCreate()方法。但是如果我使用手机上的“后退”按钮——一切正常(只通过onResume()方法),有人能解释我的代码哪里错了吗 在MainActivity.java中:Android 如何返回到以前的活动,android,android-activity,Android,Android Activity,我对android上活动的生命周期有问题。我正试图从第二个活动回到第一个活动。事实上,我的代码正在这样做,而不是它应该做的方式。我在左上角有一个“后退按钮”,当我点击它时,它会通过MainActivity的onDestroy()和onCreate()方法。但是如果我使用手机上的“后退”按钮——一切正常(只通过onResume()方法),有人能解释我的代码哪里错了吗 在MainActivity.java中: @Override public boolean onCreateOptionsMenu(
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.settings_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
package com.example.garagedoor;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
在我的设置Activity.java中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.settings_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
package com.example.garagedoor;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
在AndroidManifest.xml中:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
您的问题可能与任务堆栈有关。我不确定actionbar按钮是做什么的,但我认为您应该使用
@Override public void onBackPressed()
在设置活动中或使用意向标志启动设置活动如下:
希望这有帮助。您需要添加
android:launchMode="singleTop"
到MainActivity
的清单条目。见以下问题:
请参见