Example usage for android.app FragmentTransaction addToBackStack

List of usage examples for android.app FragmentTransaction addToBackStack

Introduction

In this page you can find the example usage for android.app FragmentTransaction addToBackStack.

Prototype

public abstract FragmentTransaction addToBackStack(@Nullable String name);

Source Link

Document

Add this transaction to the back stack.

Usage

From source file:com.grepsound.activities.MainActivity.java

/**
 * This method is used to toggle between the two fragment states by
 * calling the appropriate animations between them. The entry and exit
 * animations of the text fragment are specified in R.animator resource
 * files. The entry and exit animations of the image fragment are
 * specified in the slideBack and slideForward methods below. The reason
 * for separating the animation logic in this way is because the translucent
 * dark hover view must fade in at the same time as the image fragment
 * animates into the background, which would be difficult to time
 * properly given that the setCustomAnimations method can only modify the
 * two fragments in the transaction./*from ww  w. ja  va 2s.  c om*/
 */
private void switchFragments() {
    if (mIsAnimating) {
        Log.i(TAG, "IS animating!");
        return;
    }
    mIsAnimating = true;
    if (mDidSlideOut) {
        Log.i(TAG, "Did Slide Out!");
        mDidSlideOut = false;
        getFragmentManager().popBackStack();
    } else {
        mDidSlideOut = true;

        Animator.AnimatorListener listener = new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator arg0) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.animator.slide_fragment_left, 0, 0,
                        R.animator.slide_fragment_right);
                transaction.add(R.id.move_to_back_container, mDetailsFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                mDidSlideOut = false;
            }
        };
        slideBack(listener);
    }
}

From source file:com.example.koppa.driverlicensev2.MainActivity.java

public void addClientFragment() {
    Fragment frag = null;/*ww w. j a v a 2s.  c  om*/
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (navigationView == null) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.inflateMenu(R.menu.nav_menu_client);
        toolbar.setVisibility(View.VISIBLE);
    }

    frag = new ClientFragment();

    fragmentTransaction.replace(R.id.fragment_container, frag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

From source file:com.notalenthack.blaster.CommandActivity.java

private void launchCommand(Command cmd) {

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag(LaunchCommandDialog.TAG_LAUNCH_DIALOG);
    if (prev != null) {
        ft.remove(prev);/*from   w  ww . ja v a 2  s.c o m*/
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = LaunchCommandDialog.newInstance(cmd, this);
    newFragment.show(ft, LaunchCommandDialog.TAG_LAUNCH_DIALOG);
}

From source file:com.notalenthack.blaster.CommandActivity.java

private void editCommand(Command command) {

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag(EditCommandDialog.TAG_EDIT_COMMAND_DIALOG);
    if (prev != null) {
        ft.remove(prev);//from  ww  w .  j av a2  s. co m
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = EditCommandDialog.newInstance(command, this);
    newFragment.show(ft, EditCommandDialog.TAG_EDIT_COMMAND_DIALOG);
}

From source file:io.coldstart.android.TrapListActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.GCMStatus: {
        subscribeToMessages();/*  w w  w.ja  v  a2 s  .  com*/
        return true;
    }

    case R.id.ChangeKey: {
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putBoolean("firstRun", true);
        editor.putString("APIKey", "");
        editor.putString("keyPassword", "");
        editor.commit();
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
        return true;
    }

    case R.id.PauseAlerts: {
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Delete Traps for selected host?")
                .setMessage(
                        "Are you sure you want to logout?\nYou'll no longer receive any alerts, they won't be cached on the server and the app will close.")
                .setPositiveButton("Yes, Logout", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("onOptionsItemSelected", "Logging Out");
                        (new Thread() {
                            public void run() {
                                API api = new API();

                                try {
                                    if (api.logoutGCMAccount(settings.getString("APIKey", ""),
                                            settings.getString("keyPassword", ""), securityID)) {
                                        //We successfully logged out
                                        runOnUiThread(new Runnable() {
                                            public void run() {
                                                gcmStatus.setIcon(R.drawable.ic_action_gcm_failed);
                                                Toast.makeText(getApplicationContext(),
                                                        "Succesfully logged out. Tap the GCM icon or relaunch app to login again",
                                                        Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    } else {
                                        //TODO Popup to the user that there was a problem logging out
                                        runOnUiThread(new Runnable() {
                                            public void run() {
                                                gcmStatus.setIcon(R.drawable.ic_action_gcm_failed);
                                                Toast.makeText(getApplicationContext(),
                                                        "There was a problem logging out.", Toast.LENGTH_SHORT)
                                                        .show();
                                            }
                                        });
                                    }

                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            TrapListActivity.this.invalidateOptionsMenu();
                                        }
                                    });
                                } catch (Exception e) {
                                    //TODO this is probably pretty bad!
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                }).setNegativeButton("No", null).show();
        return true;
    }

    case R.id.Settings: {
        Intent SettingsIntent = new Intent(TrapListActivity.this, SettingsFragment.class);
        this.startActivityForResult(SettingsIntent, DISPLAY_SETTINGS);
        return true;
    }

    case R.id.SeeAPIKey: {
        if (dialogFragment != null)
            dialogFragment.dismiss();

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
            Log.i("prev", "Removing");
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        dialogFragment = ViewAPIKeyDialog.newInstance(settings.getString("APIKey", ""));
        dialogFragment.setCancelable(true);
        dialogFragment.show(ft, "dialog");

        return true;
    }

    /*case R.id.Filters:
    {
        if(dialogFragment != null)
            dialogFragment.dismiss();
            
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null)
        {
            ft.remove(prev);
            Log.i("prev","Removing");
        }
        ft.addToBackStack(null);
            
        // Create and show the dialog.
        dialogFragment = HostFilterDialog.newInstance();
        dialogFragment.setCancelable(true);
        dialogFragment.show(ft, "dialog");
            
        return true;
    }*/
    }

    return false;
}

From source file:us.cboyd.android.dicom.DcmBrowser.java

public void onFileSelected(int position, ArrayList<String> fileList, File currDir) {
    // The user selected a DICOM file from the DcmListFragment
    position -= 1;//ww  w.j  ava  2 s  .  co m
    if ((position < 0) || (position > fileList.size())) {
        // TODO: Error
        return;
    }

    if (mFragmented && mListFragment.isVisible()) {
        // If we're in the one-pane layout and need to swap fragments

        // Enable the Home/Up button to allow the user to go back to 
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

        // Create fragment and give it an argument for the selected article
        Bundle args = new Bundle();
        args.putInt(DcmVar.POSITION, position);
        args.putStringArrayList(DcmVar.FILELIST, fileList);
        args.putString(DcmVar.CURRDIR, currDir.getPath());
        mInfoFragment.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, mInfoFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(null).commit();

        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(mListFragment.getListAdapter());
    } else {
        // If we're in the two-pane layout or already displaying the DcmInfoFragment

        // Call a method in the DcmInfoFragment to update its content
        mInfoFragment.updateDicomInfo(position, fileList, currDir.getPath());
    }
    setTitle(fileList.get(position));
}

From source file:org.kymjs.kjframe.KJActivity.java

/**
 * Fragment?/*from w w  w.  j ava  2  s.  c  om*/
 *
 * @param resView        ??
 * @param targetFragment ??Fragment
 * @param isBack  ??true
 */
public void changeFragment(int resView, KJFragment targetFragment, boolean isBack) {
    if (targetFragment.equals(currentKJFragment)) {
        return;
    }
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    if (!targetFragment.isAdded()) {
        transaction.add(resView, targetFragment, targetFragment.getClass().getName());
    }
    if (targetFragment.isHidden()) {
        transaction.show(targetFragment);
        targetFragment.onChange();
    }
    if (currentKJFragment != null && currentKJFragment.isVisible()) {
        transaction.hide(currentKJFragment);
    }
    if (isBack) {
        transaction.addToBackStack(null);
    }
    currentKJFragment = targetFragment;
    transaction.commit();
}

From source file:org.kymjs.kjframe.KJActivity.java

/**
 * Fragment?/*  w  w w  . j  av  a  2s.c  o  m*/
 *
 * @param resView        ??
 * @param targetFragment ??Fragment
 * @param isBack  ??true
 */
public void changeFragment(int resView, SupportFragment targetFragment, boolean isBack) {
    if (targetFragment.equals(currentSupportFragment)) {
        return;
    }
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (!targetFragment.isAdded()) {
        transaction.add(resView, targetFragment, targetFragment.getClass().getName());
    }
    if (targetFragment.isHidden()) {
        transaction.show(targetFragment);
        targetFragment.onChange();
    }
    if (currentSupportFragment != null && currentSupportFragment.isVisible()) {
        transaction.hide(currentSupportFragment);
    }
    if (isBack) {
        transaction.addToBackStack(null);
    }
    currentSupportFragment = targetFragment;
    transaction.commit();
}

From source file:de.sourcestream.movieDB.controller.TVList.java

/**
 * Callback method to be invoked when an item in this AdapterView has been clicked.
 *
 * @param parent   The AdapterView where the click happened.
 * @param view     The view within the AdapterView that was clicked (this will be a view provided by the adapter)
 * @param position The position of the view in the adapter.
 * @param id       The row id of the item that was clicked.
 *///from  w  ww. j  ava 2 s.  c  om
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    activity.resetMovieDetailsBundle();
    activity.setRestoreMovieDetailsAdapterState(true);
    activity.setRestoreMovieDetailsState(false);
    activity.setOrientationChanged(false);
    activity.resetCastDetailsBundle();
    activity.resetTvDetailsBundle();
    if (tvDetails != null && lastVisitedTV == tvList.get(position).getId() && tvDetails.getTimeOut() == 0) {
        // Old movie details retrieve info and re-init component else crash
        tvDetails.onSaveInstanceState(new Bundle());
        Bundle bundle = new Bundle();
        bundle.putInt("id", tvList.get(position).getId());
        Bundle save = tvDetails.getSave();
        tvDetails = new TVDetails();
        tvDetails.setTimeOut(0);
        tvDetails.setSave(save);
        tvDetails.setArguments(bundle);
    } else
        tvDetails = new TVDetails();

    lastVisitedTV = tvList.get(position).getId();
    tvDetails.setTitle(tvList.get(position).getTitle());
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putInt("id", tvList.get(position).getId());
    tvDetails.setArguments(bundle);
    transaction.replace(R.id.frame_container, tvDetails);
    // add the current transaction to the back stack:
    transaction.addToBackStack("TVList");
    transaction.commit();
    fragmentActive = true;
    activity.getTvSlideTab().showInstantToolbar();
}

From source file:com.mobile.syslogng.monitor.MainActivity.java

public void setFragment(Fragment fragment, Integer position, String tag) {

    updateDrawer(position);//from  w  w  w . j a  va 2  s  . c  om
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Bundle args = new Bundle();
    args.putInt(MainActivity.FRAGMENT_POS, position);
    fragment.setArguments(args);
    String cTag = "empty";
    Fragment currentFragment = fragmentManager.findFragmentById(R.id.container);
    if (currentFragment != null) {
        cTag = currentFragment.getTag();
        if (!cTag.equals(tag)) {
            if (currentFragment instanceof WelcomeFragment
                    || currentFragment instanceof MonitoredSyslogngFragment
                    || currentFragment instanceof AboutFragment) {
                stackCount = 0;
                transaction.addToBackStack(cTag);
                stackCount++;
            } else {
                if (stackCount < 2) {
                    transaction.addToBackStack(cTag);
                    stackCount++;
                }
            }
        }
    }
    transaction.replace(R.id.container, fragment, tag).commit();
}