How to set activity no history parameter only in some cases?
android, java, themes
Solution
Call `finish()` after doing `startActivity()` to remove previous activity from the stack.
Like so:
if (isCorrespondingKiboAppInstalled) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(kiboAppPackageName);
startActivity(launchIntent);
finish();
}
Problem
I have the following configuration, I have one application that is the Main app, and another application that is considered as a Theme of the first app. The second application can't function without the first one. Now, I have to deal with two cases: User installs the second app while he already has the first Main app. User installs the second app but he does not have the first Main app. In the first case when the users run the second app (Theme), I want to redirect him to the first app (Main) as it's already installed. In this case I would like to remove the activity of the second app (Theme) from the stack. So that if users presses back, it will return him to the android desktop instead of the second (Theme) app. In this case the user should not be aware that there was an activity in the middle. In the second case I would like to run the activity of the second app (Theme). This activity has a button that redirect the user to go to google play and install the first app (main). In this case if the user decides to press the back button he will go from google play, back to the activity of the second app (Theme), so in this case I want the user see this activity and it's should be located in the stack as opposed to the first case. This is my current activity code: ``` public class MainActivity extends Activity { private Context mContext; private RelativeLayout rlShareAndWin; private TextView tvInPrizeTitle, tvPrizeDesc; private boolean isCorrespondingKiboAppInstalled; private String kiboAppPackageName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initControllers(); initControllerListeners(); } @Override protected void onResume() { super.onResume(); String themePackageName = getPackageName(); kiboAppPackageName = themePackageName.substring(0, ( themePackageName.indexOf(getResources().getString(R.string.theme_away_suffix_string)))); boolean isCorrespondingKiboAppInstalled = isKiboAppInstalledOrNot(kiboAppPackageName); if (isCorrespondingKiboAppInstalled) { Intent launchIntent = getPackageManager().getLaunchIntentForPackage(kiboAppPackageName); //launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(launchIntent); } } private void initControllers() { mContext = this; Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica LT 97 Black Condensed Oblique_0.ttf"); setContentView(R.layout.layout_activity_main); rlShareAndWin = (RelativeLayout) findViewById(R.id.rlShareAndWin); tvInPrizeTitle = (TextView) findViewById(R.id.tvInPrizeTitle); tvPrizeDesc = (TextView) findViewById(R.id.tvPrizeDesc); tvInPrizeTitle.setTypeface(typeface); tvPrizeDesc.setTypeface(typeface); } private void initControllerListeners() { rlShareAndWin.setOnClickListener(OnClickShareAndWin); } private View.OnClickListener OnClickShareAndWin = new View.OnClickListener() { @Override public void onClick(View v) { goToPlayStoreToInstallKiboApp(kiboAppPackageName); } }; private void goToPlayStoreToInstallKiboApp(String aKiboAppPackageName) { launchGooglePlayWithPackage(mContext, aKiboAppPackageName); } public void launchGooglePlayWithPackage(Context aContext, String packageName) { try { aContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))); } catch (android.content.ActivityNotFoundException anfe) { aContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageName))); } } private boolean isKiboAppInstalledOrNot(String uri) { PackageManager pm = getPackageManager(); boolean appInstalled = false; try { pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); appInstalled = true; } catch (PackageManager.NameNotFoundException e) { appInstalled = false; } return appInstalled ; } } ``` You can see that I tried to set: `launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`, ``` launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ``` To the intent that starts the main app in case it's installed. but non of those helps me to achieve what I want.