当我单击后退按钮时,我的MainActivity重新打开。为什么会这样呢? 我在MainActivity中集成了导航抽屉,然后,当我单击后退按钮时,将重新创建MainActivity。如果我再次单击后退按钮,该应用程序将关闭。
这是我的MainActivity代码:
public class BuildingsActivity extends AppCompatActivity implements BuildingsNavigator,
BuildingItemNavigator, NavigationView.OnNavigationItemSelectedListener {
private ActivityBuildingsBinding binding;
private BuildingsViewModel viewModel;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_buildings);
viewModel = new BuildingsViewModel(this);
viewModel.loadBuildings();
viewModel.dataLoading.set(true);
if (!AppUtils.isOnline()) {
startForResult();
}
binding.setViewModel(viewModel);
binding.recycler.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));
binding.recycler.setAdapter(new BuildingAdapter(new ArrayList<Building>(0), this, this));
drawerLayout = binding.drawer;
toolbar = binding.actionToolbar;
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
navigationView = binding.buildingNavigation;
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.building_nav_item:
break;
case R.id.delivery_nav_item:
Intent deliveryIntent = new Intent(this, new DeliveryActivity().getClass());
startActivity(deliveryIntent);
item.setChecked(false);
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
有什么建议吗?
答案 0 :(得分:1)
尝试添加到清单中:android:noHistory =“ true”
<activity android:name=".MainActivity"
android:screenOrientation = "portrait"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
或者您可以为此按钮编写处理程序
@Override
public void onBackPressed() {
//add what you need, for example if you want to start another activity:
Intent intent = new Intent(AnotherActivity.this, MainActivity.class);
//or if you want to close:
this.finish();
//or
finish();
}
或者如果您关于ActionBar上的按钮,则可以在菜单
中编写它@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item:
//something
return true;
case android.R.id.home:
//what you need
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
您来自的上一个活动具有一个代码,可通过其 onstart()方法将其发送回您当前所在的活动。在输入当前活动之前,请尝试完成上一个活动
@Override
protected void onStart() {
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (mFirebaseUser != null) {
Intent mIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mIntent);
}
super.onStart();
}
尝试在身份验证后完成活动
@Override
protected void onStart() {
FirebaseUser mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (mFirebaseUser != null) {
Intent mIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mIntent);
finish();
}
super.onStart();
}