2

if i press back on home fragment then show white screen and after again press application is finish, I call home fragment in Container_activity Oncreate method .I want when i press back on home fragment application will be close and do not show blank screen.here is my code:-

public class Home_fragment extends Fragment {

    ViewFlipper viewFlipper;
    int[] images = {R.drawable.home_first, R.drawable.home_second, R.drawable.home_third};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.home_fragment, container, false);

        viewFlipper = (ViewFlipper) view.findViewById(R.id.simpleViewFlipper);
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(images[i]);
            viewFlipper.addView(imageView);
        }

        Animation in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_out_right);

        viewFlipper.setInAnimation(in);
        viewFlipper.setOutAnimation(out);
        viewFlipper.setFlipInterval(3000);
        viewFlipper.setAutoStart(true);
        return view;
    }
}

Activity Class:-

public class Container_Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    FrameLayout frameLayout;
    static Container_Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container_);

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Window window = this.getWindow();

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(this.getResources().getColor(R.color.statusbarcolor));

        }
        activity = this;

        Container_Activity.showFragment(new Home_fragment(), "");

        frameLayout = (FrameLayout) findViewById(R.id.container_frame);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

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


    public static void showFragment(Fragment fragment, String Tag) {
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transcation = manager.beginTransaction();
        transcation.replace(R.id.container_frame, fragment, Tag);
        transcation.addToBackStack(null);
        transcation.commit();

    }
}
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
Navjot.jassal
  • 749
  • 2
  • 10
  • 29

3 Answers3

1

Edit

while adding home fragment don't use

transcation.addToBackStack(null);

for the fragment other than home you can use

transcation.addToBackStack(null);

Remove below line from your showFragment() function

 transcation.addToBackStack(null);
N J
  • 27,217
  • 13
  • 76
  • 96
  • if i remove this line when i press back on any fragment application will be close in every fragment thats why i use addtoBackStack – Navjot.jassal May 04 '17 at 08:33
  • then while adding home fragment don't use `addToBackStack(null)` – N J May 04 '17 at 08:34
  • when i press back from home fragment i want show toast or alert dialog for confirmation for app close. but the problem is app close when i press back from any fragment. – Navjot.jassal May 18 '17 at 11:00
  • my question is how to set condition " if(specific fragment) " show then alert dialog show to close for app – Navjot.jassal May 18 '17 at 11:57
  • 1
    Please go through link http://stackoverflow.com/questions/6750069/get-the-current-fragment-object – N J May 18 '17 at 17:39
1

For you requirement transcation.add instead off transcation.replace .

fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

transcation.add(R.id.container_frame, fragment, Tag);

Read Difference between add(), replace()

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

fragmentTransaction.addToBackStack() //dont include this for your first fragment.//

if(getSupportFragmentManager().getBackStackEntryCount() !=1){
                fragmentTransaction.addToBackStack("placeholder");
            }