当我按下后退导航按钮时活动重新启动



所以我基本上为我的通知实现了一些后按方法,我试图意图适当的活动,当一个按下后退按钮时,它会让他进入主要活动的选项卡。当用户登录但当一个人未登录主活动时,它工作正常。它有一个函数 发送启动 将人重定向到登录页面。在我按下后退按钮时开始活动。活动会不断重新启动,而不是在后台进行。我试图覆盖那里的后压方法,但失败了。

我的主活动代码看起来像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mRootRef = FirebaseDatabase.getInstance().getReference();
mToolbar = (Toolbar) findViewById(R.id.main_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Chit Chat");
if (mAuth.getCurrentUser() != null) {

mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}

//Tabs
mViewPager = (ViewPager) findViewById(R.id.tab_pager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.main_tab);
mTabLayout.setupWithViewPager(mViewPager);

ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
mUserRef.child("online").setValue(ServerValue.TIMESTAMP);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
//onstart action here
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null && currentUser.isEmailVerified()){
//                    intentExtra = getIntent().getStringExtra("backPressed");
mUserRef.child("online").setValue("true");

} else {
sendToStart();
}

}

});

其中 sendtoStart 是:

private void sendToStart() {
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startIntent);
finish();
}

这就是我根据意图值切换到选项卡的方式:

@Override
protected void onResume() {
super.onResume();
if(intentExtra != null) {
if (intentExtra.equals("notification")) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tab);
TabLayout.Tab tab = tabLayout.getTabAt(1);
tab.select();

}

}
}

如果您需要任何编码部分,请告诉我。我已经尝试了从在互联网上找到解决方案到调试自己的所有方法,但找不到修复程序。

试试这个:

Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
// set the new task and clear flags
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startIntent);
finish();

最新更新