How to run an activity only once like Splash screen

This is how I achieved it!Hope it helps! import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; public class check extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); SharedPreferences settings=getSharedPreferences(“prefs”,0); boolean firstRun=settings.getBoolean(“firstRun”,false); if(firstRun==false)//if running for first time //Splash will load for first time { SharedPreferences.Editor editor=settings.edit(); editor.putBoolean(“firstRun”,true); editor.commit(); Intent i=new … Read more

get the latest fragment in backstack

You can use the getName() method of FragmentManager.BackStackEntry which was introduced in API level 14. This method will return a tag which was the one you used when you added the Fragment to the backstack with addTobackStack(tag). int index = getActivity().getFragmentManager().getBackStackEntryCount() – 1 FragmentManager.BackStackEntry backEntry = getFragmentManager().getBackStackEntryAt(index); String tag = backEntry.getName(); Fragment fragment = getFragmentManager().findFragmentByTag(tag); … Read more

How to pop fragment off backstack

You can pop the fragment by name. While adding fragments to the back stack, just give them a name. fragmentTransaction.addToBackStack(“fragB”); fragmentTransaction.addToBackStack(“fragC”); Then in Fragment_C, pop the back stack using the name ie.. fragB and include POP_BACK_STACK_INCLUSIVE someButtonInC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getActivity() .getSupportFragmentManager(); fm.popBackStack (“fragB”, FragmentManager.POP_BACK_STACK_INCLUSIVE); } });

Android: open activity without save into the stack

When starting your list’s Activity, set its Intent flags like so: Intent i = new Intent(…); // Your list’s Intent i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag startActivity(i); The FLAG_ACTIVITY_NO_HISTORY flag keeps the new Activity from being added to the history stack. NB: As @Sam points out, you can use i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); instead. There is … Read more

Android: Fragments backStack

If you really want to replace the fragment then use replace() methode instead of doing a remove() and an add(). FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(…………..); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); Don’t forget to do the addToBackStack(null) so your previous state will be added to the backstack allowing you to go back with the back … Read more

Fragments onResume from back stack

For a lack of a better solution, I got this working for me: Assume I have 1 activity (MyActivity) and few fragments that replaces each other (only one is visible at a time). In MyActivity, add this listener: getSupportFragmentManager().addOnBackStackChangedListener(getListener()); (As you can see I’m using the compatibility package). getListener implementation: private OnBackStackChangedListener getListener() { OnBackStackChangedListener … Read more

Android: Remove all the previous activities from the back stack

The solution proposed here worked for me: Java Intent i = new Intent(OldActivity.this, NewActivity.class); // set the new task and clear flags i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); Kotlin val i = Intent(this, NewActivity::class.java) // set the new task and clear flags i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(i) However, it requires API level >= 11.