How can I restart my application or MainActivity when the activity have:
android:launchMode="singleTop"
I have tried:
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
But because of singleTop this not working, there is an other way to do this?
I did my theme switcher like this:
Intent intent = getIntent();
finish();
startActivity(intent);
Basically, I'm calling finish() first, and I'm using the exact same intent this activity was started with. That seems to do the trick?
UPDATE: As pointed out by Ralf below, Activity.recreate() is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to up-vote Ralf's answer!)
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
or
private void restartFirstActivity()
{
Intent i = getApplicationContext().getPackageManager()
.getLaunchIntentForPackage(getApplicationContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(i);
}
try to restart with this may this will help u
Intent i = cntxt.getPackageManager().getLaunchIntentForPackage(cntxt.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cntxt.startActivity(i);
Use this:
Intent finishIntent = new Intent( this,
activity_that_you_want_to_return.class);
finishIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(finishIntent);
Related
I'm trying to use a button to change from Main Activity on android studio to Main Activity 2 and I get the error
no suitable constructor found for Intent(<anonymous OnClickListener>,Class<MainActivity2>)
Intent intent = new Intent(this, MainActivity2.class);
^
I'm on version 4.1 and I want to assume i'm following an old tutorial or I just missed some punctuation.
This is my code:
buttonPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity2();
}
public void openMainActivity2(){
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
Just to give more clarity to Caio's answer, when you use this
Intent intent = new Intent(this, MainActivity2.class);
The intent is created in an anonymous inner class i.e. OnClickListener. Thus this does not refer the instance of your Activity (or Context) as intended. You need to provide the correct context of your class.
Hence , do this:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
You have to try getApplicationContext() or activity.this instead of this.
I think you are having this issue for "this". Cause, I can't see any other issue.
Intent intent = new Intent(getApplicationContext(),MainActivity2.class)
startActivity(intent);
you should provide the correct context of your class.
try this:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
This java code is for the old version, it does not work with android studio's latest version.
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivities(intent);
here is code for the more recent versions:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openMainActivity();
}
public void openMainActivity() {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
};
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 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();
I want to start another activity in finish() depending on a condition. I got the same code working in onDestroy() but I think this is not the right place from the lifecycle point of view.
(Activities might be destroyed, although there were not actively left by the user).
The following code did not have any effect:
#Override
public void finish() {
if (mCondition) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
super.finish();
}
Why is it not working, are there alternatives?
I think the best option is starting the new activity then finish the current one:
if (mCondition) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
finish();
}
I have Used Myself this and it is working fine
public void ToBeClosed()
{
Intent a = new Intent(context,NewActivity.class);
startActivity(a);
OldActivity.this.finish();
}
and use the function in if statement body
if (mCondition) { ToBeClosed(); }
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();