Clearning Stack of Activities unto the Top - java

I want to perform a logout function in which I want to clear all the activities before the logout and start a new Login Activity
Here is my code
Utilities.logoutPlayerDefaults(Profile.this);
Utilities.vibrate(Profile.this);
Intent myIntent = new Intent (Profile.this,FBLogin.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
But it doesn't work. If i press back button i go back to Profile

Try following way,
#Override
public void onBackPressed()
{
Intent myIntent = new Intent (Profile.this,FBLogin.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
super.onBackPressed();
}

Change your code to below :
Utilities.logoutPlayerDefaults(Profile.this);
Utilities.vibrate(Profile.this);
Intent myIntent = new Intent (Profile.this,FBLogin.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();

Related

How to exit onBackPressed method in android studio?

I have developed an android apps that have a splash screen and a login page. The problem is when a user give credential and pressed log in button it open controlActivity. but when I pressed back button, I navigate to login page again If I pressed back button again I navigate to splash screen. How can I stop navigating to previous page, when user press back button then app should exit?
Why does it happen ?
This is happening because whenever you open another activity from intent, it creates it over it.
How to solve the problem?
You can add a flag like this
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
OR
You can finish it.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
If you want to close the app onBackPressed, you can override the method. Add this to your activity.
#Override
public void onBackPressed(){
finish();
System.exit(0);
}
Doing this, the app will be closed on back pressed!
You need to put a flag in the activity you want to be forgotten.
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Answer by #Sambhav Khandelwal should solve your problem. In your splash screen you need to do add flags with intent like:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class); //assuming your splash screen name as SplashScreenActivity and login as LoginActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Or you can do without flags like below:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
startActivity(intent);
finish();

How do go to Activity form a recycleview fragment

enter image description here
I am in home fragment currently and needed to go to pdf activity .
For Transaction Between fragmnet and activity you can simply use intent.
Intent intent = new Intent(getActivity(), mFragmentFavorite.class);
startActivity(intent);
From an Activity
Intent intent = new Intent(this, mFragmentFavorite.class);
startActivity(intent);
On line number 90, instead of MainActivity.this, simply write this or getActivity()
new Intent(this, mFragmentFavorite.class)
Or
new Intent(getActivity(), mFragmentFavorite.class)

Using intent to go back previous page instead go in to the previous page...

I'm using intent go back to the previous page and put finish(); But It goes like B page go in A page instead of B page back to A page. Do I type wrong code?
Here is the code
Intent intent = new Intent(Donation_Page.this, MainActivity.class);
startActivity(intent);
finish();
Change your code from
Intent intent = new Intent(Donation_Page.this, MainActivity.class);
startActivity(intent);
finish();
to
finish();
That's it
you can also try #Override onBackPressed to go back to previous Activity
#Override
public void onBackPressed() {
super.onBackPressed();
}

I want to pass flag name to other activity and to show each web view if equal flag

I have two main activities.In login activity ,if I clicked login button to pass flag to main activity.In main activity,if equal flag go to login page of web view .if not so,stay home page.When I created it,pass flag variable is ok but if I clicked login button ,it always go to home page.Please help me kindly.I am learning android as a beginner.
piece of code of MainActivity.java
String flag = getIntent().getStringExtra("flag");
Log.d("FlagCheck", "this flag is (" + flag + ")");
if (flag=="add"){
webview.loadUrl(login_address);
Log.d("FlagCheck", "this flag is login_address");
}else{
webview.loadUrl(home_address);
Log.d("FlagCheck", "this flag is home_address");
}
piece of code of LoginActivity.java
mEmailSignInButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
savePreferences("CheckBox_Value", checkBoxRememberMe.isChecked());
if (checkBoxRememberMe.isChecked())
savePreferences("storedName", mEmailView.getText().toString());
savePreferences("storedPass", mPasswordView.getText().toString());
/* int requestCode = 1001;
Intent intent = new Intent( getApplicationContext(), MainActivity.class );
startActivityForResult( intent, requestCode );
intent.putExtra( "key", "result_val" );
setResult( Activity.RESULT_OK, intent );*/
finish();
// startActivity(new Intent(LoginActivity.this, HomeActivity.class));
// finish();
Intent i= new Intent(LoginActivity.this, MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("flag", "add");
startActivity(i);
}
});
You should use str.equals("add") or str.equalsIgnoreCase().
As == doesn't checks whether both strings are equal or not, it compares string's location.
You can click here for more information about it.
Try this
if (flag.equalsIgnoreCase("add")){
webview.loadUrl(login_address);
Log.d("FlagCheck", "this flag is login_address");
}else{
webview.loadUrl(home_address);
Log.d("FlagCheck", "this flag is home_address");
}
USed this...
finish();
Intent i= new Intent(LoginActivity.this, MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("flag", "add");
startActivity(i);
Instead of (Remove Finish to above)
Intent i= new Intent(LoginActivity.this, MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("flag", "add");
startActivity(i);
finish()
You Have Value pass the add and or other than you send also like that
if (flag.equalsIgnoreCase("add")){
Intent i= new Intent(LoginActivity.this, MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("flag", "add");
startActivity(i);
finish()
}
else
{
Intent i= new Intent(LoginActivity.this, MainActivity.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("flag", "home");
startActivity(i);
finish()
}

How do I implement a Button that will return to "Home" after it is clicked?

I have created a "Button" in android studio to send emails. When I click it, it sends an email as intended, but does not then return to the "Home" activity.
You can use finish(); method, but it's not recommended.
See: How to finish current activity in Android
http://developer.android.com/guide/components/processes-and-threads.html#Threads
But, this is my suggestion, you should be able to after clicking the Button, then clear the activity and restart it(or you can do something like Gmail App and doing some stuffs after clicking):
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Also you can use:
Intent m = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(m);
Or:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

Categories