1

I've been trying the new Navigation component and so far so good. However, I've hit a wall when it comes to the following. My question is best described with an example, so let me give one.

I think it's a very common scenario that apps have a login screen and then they forward the user to a home screen, or dashboard, or something similar. Let's stick to the naming - LoginScreen and HomeScreen.

Say the app is very simple. It starts at the LoginScreen and as soon as the user logs in, they're forwarded to the HoneScreen. Pretty simple app, but serves the example.

Now, if the user hits back, how do we exit the app? So far it always takes me to the LoginScreen.

Further evolving on this example. Imagine there's a welcome screen before the login screen where the user can decide to login or register. How would one deal with navigating back in this case too?

Essentially, I'm asking if there's a built in functionality in the navigation component that let's you go back more than one screen and if not, is there a way to achieve this? Thank you very much.

3 Answers 3

1

@Fred

To achieve this you can finish each activity after having started the other one. For example the WelcomeScreen has called LoginScreen you can directly finish it and when LoginScreen calls HomeScreen finish LoginScreen so when the user will navigate back from HomeScreen all activities will be closed.

Or, from the present activity you can call System.exit(0) to exit the application. This serves when you have many activities but if you have one there will be no difference with finish()

Another scenario is as you described in comments: Consider you have following activities: ActivityA, ActivityB and ActivityC and you want to come back to ActivityA from ActivityC without passing by ActivityB. If you ActivityA has been previously opened you can bring it back to front, depending on when you want this to be done. With our sample we consider when the activity finishes:

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //this will bring the ActivityA on the front

        Intent intent=new Intent(this,ActivityA.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);

    }

Or you may have a different scenario where the LoginActivity is started only if the is not already connected otherwise you do directly to HomeScreen. In this case, I create a Singleton that will help me register the current calling activity so that I can know which activity will be directly started after successful login.

For example:

if(sharedPreferences!=null) {
                    if (sharedPreferences.getBoolean("user_connected",false)){
                        startActivity(new Intent(this, HomeScreen.class));}
                    else
                    {
                        MySingleton.getInstance().setCurrentCallingActivity("HomeScreen");
                        startActivity(new Intent(this, LoginActivity.class));
                    }
                }

And once in LoginActivity, after a successful login, I do the following:

 try
                                {
                                    startActivity(new Intent(this,
                                            getClassLoader().loadClass(MySingleton.getInstance().getCurrentCallingActivity())));
                                    finish();
                                }catch (ClassNotFoundException e)
                                {
                //your code
                                    finish();
                                }

That's how I handle that and it works. Up to you to see a corresponding scenario and try if it can work. Different approches may coexist and this far from being performant.

7
  • I understand that finishing the activity would be a way to achieve what is needed in this scenario, but as I asked, what if you want to remain in the app? Does this mean you'd have to create multiple activities?
    – Fred
    Oct 14, 2018 at 7:30
  • I mean, if you have the following ScreenA -> ScreenB -> ScreenC and you hit back while on ScreenC is there a built-in way in the navigation component to go back to ScreenA
    – Fred
    Oct 14, 2018 at 12:13
  • 1
    No no, you have to implement that yourself unfortenately. I've done this before if you want I can share the code. But within 2hours once back home Oct 14, 2018 at 12:15
  • yeah, that'd be great
    – Fred
    Oct 14, 2018 at 12:16
  • Sorry fo being late, I edited my answer. take a look please Oct 14, 2018 at 15:18
1

You could try this:

Intent myIntent = new Intent(LoginScreen.this, HomeScreen.class);
startActivity(myIntent);
finish(); //finish LoginScreen, then when press back in HomeScreen, it will exit instead return LoginScreen
1
  • I understand that finishing the activity would be a way to achieve what is needed in this scenario, but as I asked, what if you want to remain in the app? Does this mean you'd have to create multiple activities?
    – Fred
    Oct 14, 2018 at 7:30
0

First of all you need to see if u prefer to use Activity or Fragments.

if you want to remove all Fragments in the stack then you need to use

 FragmentManager fms = getSupportFragmentManager();
 for (int i = 0; i < fms.getBackStackEntryCount(); ++i) {
                        fms.popBackStack(); }

If you want to remove specifc Fragment than you will have to use tags.

Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();

Now about Activity you need to consider a few options

Activity A is your Login. Activity B is your Register page. Activity C is your Homepage.

if you want to move from A to C after Successful login than use

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();

if you want to move from A to B

Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);

if you want to move from A to B and after Register success move to C than use :

Intent intent = new Intent(LoginSM.this, MainActivitySM.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
1
  • Thanks for the answer and time spent. Unfortunately, I'm already aware of how to deal with those. The question is if there's a built-in way in the navigation component recently released by Google.
    – Fred
    Oct 14, 2018 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.