简体   繁体   中英

Activity Restarting when I press back navigation button

So I basically I have implemented some on back pressed method for my notification where I am trying to intent appropriate activity and when one press back button it gets him to tabs of the main activity. It is working fine when the user is logged in but when a person is not logged in the MainActivity. it has a function sendtostart which redirects the person to login page. On that Start activity when I press the back button. Activity keeps on restarting itself rather than going in the background. I have tried to override the on back pressed method over there but failed.

My code of MainActivity Looks like this:

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

                }


                }


        });

where sendtoStart is:

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

}

This is how I am shifting to Tabs based on intent value:

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


            }


            }
        }

Please let me know if any coding part you want. I have tried everything from finding a solution on the Internet to Debugging my self but couldn't find the fix.

Try with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM