简体   繁体   中英

How to keep a TextView and a Button invisible after starting an Intent

So I have a MainActivity on which I have "Login" button, a "Take Quiz" button and "Not registered? Click here" TextView. If the user logs in, the "Login" button and the TextView go invisible and a "My Account" button becomes visible. Let's say the user decides to log in before taking the quiz. Everything works as it should be - the "Login" button and the TextView go invisible and a "My Account" button becomes visible. After that the user decides to take the quiz, which takes them to another activity. After they take the quiz, they are taken to a third activity - ResultActivity. There is a "menu" item which takes them back to MainActivity with an intent:

 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId())
        {
            case android.R.id.home:
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                break;
        }
        return true;
    }

And now here is the problem. When the user goes back to MainActivity, instead of "My Account" button, there are the "Log in" button and the TextView to register. How can I keep them invisible and leave the "My Account" button visible?

I should probably use savedInstanceState but I have no idea how because every tutorial online includes only text in an EditText or a Textview. I apologize for the long question.

That is because you are creating a new Activity instead of "going back" to the first one. This menu thing should pop the stack until you hit the MainActivity.

I recommend to use fragments instead of so many activities.

You could create a boolean variable like isAuthenticated and set it to true when the user is authenticated and change it back to false when the user is not authenticated. You could also store the boolean variable with sharedPreferences. So whenever you go back to your MainActivity you could get that boolean variable from shared preferences and check if it's true or false. So if isAuthenticated is true make the login button invisible, else make it visible.

Another way is to authenticate with Firebase which in my opinion is better.

You can store the state of it in a variable the check it and act accordingly.
ex:-You can use the SharedPreferences
https://stackoverflow.com/a/35554369/5642890

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