Reputation: 6166
Is it possible to start an implicit activity on the stack, by clearing the entire history before it?
I am creating an application which have a concept of force upgrade from playsotre. therefore I want to open play store at some moment and same moment I also want to close my application basically I want clear all activity of my Application after opening play store, but it didn't work.
My code snip:
btnForceUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=" + context.getPackageName()));
goToMarket.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); // clearing all activity task (Basically close the app)
mDialog.cancel();
context.startActivity(goToMarket);
}
});
I am using below flag in intent object but it didnt work
goToMarket.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK); // clearing all activity task (Basically close the app)
Upvotes: 1
Views: 658
Reputation: 3356
try {
final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finishAffinity();
startActivity(i); //
} catch (final android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
Or if you want it to work in previous versions(< 15 API) of Android:
ActivityCompat.finishAffinity(this);
Upvotes: 2
Reputation: 2707
You need to make one activity with start google play like below
public class CloseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_settings);
final String appPackageName = getPackageName();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
finish();
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
finish();
}
}
}
Need to start this activity from class with below code:
Intent mIntent = new Intent(CurrentActivity.this,CloseActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mIntent);
finish();
Upvotes: 1