Java Code Examples for android.app.FragmentTransaction#addToBackStack()

The following examples show how to use android.app.FragmentTransaction#addToBackStack() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ErrorFragment.java    From ShoppingList with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View view) {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    switch (view.getId()) {
        case R.id.error_btn_retry:
            ShoppingListFragment listFR = new ShoppingListFragment();
            transaction.replace(R.id.fragment_container, listFR);
            transaction.addToBackStack(null);
            transaction.commit();
            break;
        case R.id.error_btn_cache:
            CacheListFragment cacheFR = new CacheListFragment();
            transaction.replace(R.id.fragment_container, cacheFR, "CACHE_FRAGMENT");
            transaction.addToBackStack(null);
            transaction.commit();
            break;
        case R.id.error_btn_settings:
            Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);

    }
}
 
Example 2
Source File: MapsAppActivity.java    From maps-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Opens the map represented by the specified portal item or if null, opens
 * a default map.
 *
 * @param portalItemId
 *            - String representing a portal resource
 * @param basemapPortalItemId
 *            - String representing a basemap portal item
 */
public void showMap(String portalItemId, String basemapPortalItemId) {
	// remove existing MapFragment explicitly, simply replacing it can cause
	// the app to freeze when switching basemaps
	FragmentTransaction transaction;
	FragmentManager fragmentManager = getFragmentManager();
	Fragment currentMapFragment = fragmentManager.findFragmentByTag(MapFragment.TAG);
	if (currentMapFragment != null) {
		transaction = fragmentManager.beginTransaction();
		transaction.remove(currentMapFragment);
		transaction.commit();
	}

	MapFragment mapFragment = MapFragment.newInstance(portalItemId, basemapPortalItemId);

	transaction = fragmentManager.beginTransaction();
	transaction.replace(R.id.maps_app_activity_content_frame, mapFragment, MapFragment.TAG);
	transaction.addToBackStack(null);
	transaction.commit();

	invalidateOptionsMenu(); // reload the options menu
}
 
Example 3
Source File: MirrorActivity.java    From mirror with Apache License 2.0 6 votes vote down vote up
private void showFragment(final FragmentManager fragmentManager,
                          final Fragment fragment,
                          final boolean addToBackStack,
                          final String tag) {
    // hide the full screen container
    hideFullScreenView();

    if (fragmentManager.findFragmentByTag(tag) == null) {
        final int viewId = mContentContainer.addBorderView(this);
        updateCurrentPresenterClass(fragment, viewId);

        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(viewId, fragment, tag);
        if (addToBackStack) {
            transaction.addToBackStack(tag);
        }
        transaction.commitAllowingStateLoss();
    } else {
        fragmentManager
                .beginTransaction()
                .detach(fragment)
                .attach(fragment)
                .commitAllowingStateLoss();
    }
}
 
Example 4
Source File: CpuPreferenceFragment.java    From android-kernel-tweaker with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onPreferenceClick(Preference pref) {
	// TODO Auto-generated method stub
	Fragment f = null;
	if(pref == mAdvancedGovernor) {
		f = new CpuGovernorPreferenceFragment();
	}
	if(pref == mAdvancedCpuquiet) {
		f = new CpuquietGovernorPreferenceFragment();
	}
	FragmentTransaction ft = getFragmentManager().beginTransaction();
	// This adds the newly created Preference fragment to my main layout, shown below
	ft.replace(R.id.activity_container,f);
	// By hiding the main fragment, transparency isn't an issue
	ft.addToBackStack("TAG");
	ft.commit();


	return false;
}
 
Example 5
Source File: RssfeedActivity.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
    public void onRssItemSelected(String link) {
        if (getResources().getBoolean(R.bool.twoPaneMode)) {
            DetailFragment fragment = (DetailFragment) getFragmentManager()
                    .findFragmentById(R.id.detailFragment);
            fragment.setText(link);
        } else {
// replace the fragment
// Create fragment and give it an argument for the selected article
            DetailFragment newFragment = new DetailFragment();
            Bundle args = new Bundle();
            args.putString(DetailFragment.EXTRA_URL, link);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);
// Commit the transaction
            transaction.commit();
        }
    }
 
Example 6
Source File: ConversationsActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void initializeFragments() {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
    Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
    if (mainFragment != null) {
        if (binding.secondaryFragment != null) {
            if (mainFragment instanceof ConversationFragment) {
                getFragmentManager().popBackStack();
                transaction.remove(mainFragment);
                transaction.commit();
                getFragmentManager().executePendingTransactions();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.secondary_fragment, mainFragment);
                transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
                transaction.commit();
                return;
            }
        } else {
            if (secondaryFragment instanceof ConversationFragment) {
                transaction.remove(secondaryFragment);
                transaction.commit();
                getFragmentManager().executePendingTransactions();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.main_fragment, secondaryFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return;
            }
        }
    } else {
        transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
    }
    if (binding.secondaryFragment != null && secondaryFragment == null) {
        transaction.replace(R.id.secondary_fragment, new ConversationFragment());
    }
    transaction.commit();
}
 
Example 7
Source File: HelpUtils.java    From BetterWeather with Apache License 2.0 5 votes vote down vote up
public static void showAboutDialog(Activity activity) {
    FragmentManager fm = activity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog_about");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    new AboutDialog().show(ft, "dialog_about");
}
 
Example 8
Source File: CheckingFragment.java    From android-sholi with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_edit:
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.container, new EditFragment());
            transaction.addToBackStack(null);
            transaction.commit();
            return true;
        case R.id.action_menu:
            getActivity().openContextMenu(_listView);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 9
Source File: MirrorActivity.java    From mirror with Apache License 2.0 5 votes vote down vote up
private void showDialogFragment(final FragmentManager fragmentManager,
                                final DialogFragment dialogFragment,
                                final boolean addToBackStack,
                                final String tag) {
    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(dialogFragment, tag);
    if (addToBackStack) {
        transaction.addToBackStack(tag);
    }
    transaction.commitAllowingStateLoss();
}
 
Example 10
Source File: MainActivity.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private void onWaypointProperties(Waypoint waypoint) {
    mEditedWaypoint = waypoint;
    Bundle args = new Bundle(2);
    args.putString(WaypointProperties.ARG_NAME, mEditedWaypoint.name);
    args.putInt(WaypointProperties.ARG_COLOR, mEditedWaypoint.style.color);
    Fragment fragment = Fragment.instantiate(this, WaypointProperties.class.getName(), args);
    fragment.setEnterTransition(new Fade());
    FragmentTransaction ft = mFragmentManager.beginTransaction();
    ft.replace(R.id.contentPanel, fragment, "waypointProperties");
    ft.addToBackStack("waypointProperties");
    ft.commit();
    updateMapViewArea();
}
 
Example 11
Source File: MainActivity.java    From ShoppingList with Apache License 2.0 5 votes vote down vote up
private void displayCached() {
    CacheListFragment cacheFR = new CacheListFragment();
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.fragment_container, cacheFR);
    transaction.addToBackStack(null);
    transaction.commit();
}
 
Example 12
Source File: PreferenceActivity.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
/**
 * Start a new fragment.
 *
 * @param fragment The fragment to start
 * @param push If true, the current fragment will be pushed onto the back stack.  If false,
 * the current fragment will be replaced.
 */
public void startPreferenceFragment(Fragment fragment, boolean push) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.prefs, fragment);
    if (push) {
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(BACK_STACK_PREFS);
    } else {
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    }
    transaction.commitAllowingStateLoss();
}
 
Example 13
Source File: MainActivity.java    From hex with Apache License 2.0 5 votes vote down vote up
@Override
public void onItemSelectedHandler(HexDrawer.Item item) {
    String collection;

    switch (item) {
        case Settings:
            Intent settingsIntent = new Intent(getApplicationContext(), SettingsActivity.class);
            startActivity(settingsIntent);
            break;
        case About:
            Intent aboutIntent = new Intent(getApplicationContext(), AboutActivity.class);
            startActivity(aboutIntent);
            break;
        default:
            mCurrentItem = item;

            collection = itemToCollection.get(item).toString();
            Bundle bundle = new Bundle();
            bundle.putString(StoryListFragment.CollectionKey, collection);
            StoryListFragment storyListFragment = new StoryListFragment();
            storyListFragment.setArguments(bundle);

            if (getFragmentManager().getBackStackEntryCount() > 0) {
                getFragmentManager().popBackStackImmediate();
            }

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.content_wrapper, storyListFragment);
            transaction.addToBackStack(item.toString());

            transaction.commit();

            break;
    }
}
 
Example 14
Source File: FragmentStack.java    From android-test with Apache License 2.0 5 votes vote down vote up
void addFragmentToStack() {
  stackLevel++;

  // Instantiate a new fragment.
  Fragment newFragment = CountingFragment.newInstance(stackLevel);

  // Add the fragment to the activity, pushing this transaction
  // on to the back stack.
  FragmentTransaction ft = getFragmentManager().beginTransaction();
  ft.replace(R.id.simple_fragment, newFragment);
  ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  ft.addToBackStack(null);
  ft.commit();
}
 
Example 15
Source File: Utils.java    From UTubeTV with The Unlicense 5 votes vote down vote up
public static void showFragment(Activity activity, Fragment fragment, int resID, int animationType, boolean addToBackStack) {
  // check params and bail if necessary
  if (fragment == null || activity == null) {
    DUtils.log("bad params: " + DUtils.currentMethod());
    return;
  }

  FragmentManager fragmentManager = activity.getFragmentManager();
  FragmentTransaction ft = fragmentManager.beginTransaction();

  switch (animationType) {
    case 0:
      break;
    //      case 1:
    //        ft.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_left, R.animator.slide_in_right, R.animator.slide_out_right);
    //        break;
    //      case 2:
    //        ft.setCustomAnimations(R.animator.slide_in_down, R.animator.slide_out_down, R.animator.slide_in_up, R.animator.slide_out_up);
    //        break;
    //      case 3:
    //        ft.setCustomAnimations(R.animator.fraggy_enter, R.animator.fraggy_exit, R.animator.fraggy_pop_enter, R.animator.fraggy_pop_exit);
    //        break;
    default:
      ft.setCustomAnimations(R.animator.fade_enter, R.animator.fade_exit, R.animator.fade_enter, R.animator.fade_exit);
      break;
  }

  ft.replace(resID, fragment);

  if (addToBackStack)
    ft.addToBackStack(null);

  ft.commit();
}
 
Example 16
Source File: ConversationsActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void openConversation(Conversation conversation, Bundle extras) {
    ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment);
    final boolean mainNeedsRefresh;
    if (conversationFragment == null) {
        mainNeedsRefresh = false;
        Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
        if (mainFragment instanceof ConversationFragment) {
            conversationFragment = (ConversationFragment) mainFragment;
        } else {
            conversationFragment = new ConversationFragment();
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
            fragmentTransaction.addToBackStack(null);
            try {
                fragmentTransaction.commit();
            } catch (IllegalStateException e) {
                Log.w(Config.LOGTAG, "sate loss while opening conversation", e);
                //allowing state loss is probably fine since view intents et all are already stored and a click can probably be 'ignored'
                return;
            }
        }
    } else {
        mainNeedsRefresh = true;
    }
    conversationFragment.reInit(conversation, extras == null ? new Bundle() : extras);
    if (mainNeedsRefresh) {
        refreshFragment(R.id.main_fragment);
    } else {
        invalidateActionBarTitle();
    }
}
 
Example 17
Source File: MainActivity.java    From ShoppingList with Apache License 2.0 5 votes vote down vote up
private void displayFavorites() {
    FavoriteListFragment favFR = new FavoriteListFragment();
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.fragment_container, favFR);
    transaction.addToBackStack(null);
    transaction.commit();
}
 
Example 18
Source File: ConversationsActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
private void initializeFragments() {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
    Fragment secondaryFragment = getFragmentManager().findFragmentById(R.id.secondary_fragment);
    if (mainFragment != null) {
        Log.d(Config.LOGTAG, "initializeFragment(). main fragment exists");
        if (binding.secondaryFragment != null) {
            if (mainFragment instanceof ConversationFragment) {
                Log.d(Config.LOGTAG, "gained secondary fragment. moving...");
                getFragmentManager().popBackStack();
                transaction.remove(mainFragment);
                transaction.commit();
                getFragmentManager().executePendingTransactions();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.secondary_fragment, mainFragment);
                transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
                transaction.commit();
                return;
            }
        } else {
            if (secondaryFragment instanceof ConversationFragment) {
                Log.d(Config.LOGTAG, "lost secondary fragment. moving...");
                transaction.remove(secondaryFragment);
                transaction.commit();
                getFragmentManager().executePendingTransactions();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.main_fragment, secondaryFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return;
            }
        }
    } else {
        transaction.replace(R.id.main_fragment, new ConversationsOverviewFragment());
    }

    if (binding.secondaryFragment != null && secondaryFragment == null) {
        transaction.replace(R.id.secondary_fragment, new ConversationFragment());
    }
    transaction.commit();
}
 
Example 19
Source File: WorldActivity.java    From blocktopograph with GNU Affero General Public License v3.0 4 votes vote down vote up
/** Open NBT editor fragment for the given editableNBT */
public void openNBTEditor(EditableNBT editableNBT){

    // see changeContentFragment(callback)
    this.confirmContentClose = getString(R.string.confirm_close_nbt_editor);

    EditorFragment editorFragment = new EditorFragment();
    editorFragment.setEditableNBT(editableNBT);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.world_content, editorFragment);
    transaction.addToBackStack(null);

    transaction.commit();

}
 
Example 20
Source File: MainActivity.java    From Inspeckage with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    int id = item.getItemId();

    if (id == R.id.nav_clear) {

        clearAll();
        TextView txtAppSelected = (TextView) findViewById(R.id.txtAppSelected);
        if(txtAppSelected!=null) {
            txtAppSelected.setText("... ");
        }

    } else if (id == R.id.nav_close) {

        clearAll();
        stopService();
        super.finish();
        android.os.Process.killProcess(android.os.Process.myPid());

    } else if (id == R.id.nav_config) {

        ConfigFragment configFragment = new ConfigFragment(this);
        fragmentTransaction.replace(R.id.container, configFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_auth) {

        AuthFragment authFragment = new AuthFragment(this);
        fragmentTransaction.replace(R.id.container, authFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_share) {

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "https://github.com/ac-pm/Inspeckage");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);

    }else{

        MainFragment mainFragment = new MainFragment();
        fragmentTransaction.replace(R.id.container, mainFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}