user3416113
user3416113

Reputation: 61

Creating activities architecture Android

while creating application I faced a architecture problem. I want to do like this : enter image description here

I thing everything is shown clearly. By the way. While app user is in Menu activity, and if he press back button, (not Logout method) he does not log out. I'm newbie in android, could someone give me an advice ? what methods should i use ?

                Intent menuIntent = new Intent(
                        "com.project.simplify.NewReviewActivity");
                menuIntent.putExtra("user", user);
                menuIntent.putExtra("link", realLink);
                startActivity(menuIntent);

here is the Logout method :

Intent loginIntent = new Intent(this, LoginActivity.class);
                loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(loginIntent);

                finish();

How i solved my problem :

public static MenuActivity MenuActivityObject;

onCreate()
{
    MenuActivityObject = this;
}

now use that object in logout method to finish menu activity:

Logout()
{
    MenuActivity.MenuActivityObject.finish();
}

Upvotes: 0

Views: 64

Answers (5)

androidDev
androidDev

Reputation: 36

change you logout code like this

Intent loginIntent = new Intent(this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(loginIntent);

                finish();

Upvotes: 0

androidDev
androidDev

Reputation: 36

If you want to logout when user presses back button, you can ovverride "onBackPressed" in "MenuActivity" and call "logout method".

Upvotes: 0

androidDev
androidDev

Reputation: 36

Your logout method start LoginActivity and kill the last Activity launched , Activity1 , Activity2 or Activity3.

If you don't close the MenuActivity when start the next Activity , MenuActivity remain on the stack , so when you do logout , you start LoginActivity but if user press back button the system replace LoginActivity with the last Activity present on the stack , in this case MenuActivity.

Upvotes: 0

androidDev
androidDev

Reputation: 36

This happen because you not close the manu activity when start the Activity 1.

for example

MenuActivity

oncreate{

Intent inte = new Intent(context,Activity1.class);
startActivity(inte);
finish();
}

if you don't call "finish()" the MenuActivity is still alive. Post your source code.

Upvotes: 0

Libin
Libin

Reputation: 17085

When you click on Logout from any of the Activity to launch LoginActivity use Intent.FLAG_ACTIVITY_CLEAR_TASK . This will clear the all the previous Activity in stack.

Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

When the user press BACK on MenuActivity , the system will finish the MenuActivity and user will be taken to the LoginActivity as per your architecture . It will work perfectly

Upvotes: 1

Related Questions