Fragmentdan asosiy faoliyatga qaytish uchun orqaga tugmasini bosing

Salom, ilovalar tortmasida fragmentlar yaratdim va endi men parcha ustida orqaga tugmani bosganimda dasturni yopmoqda.

MainView veb-saytida allaqachon mavjud.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (myWebView.canGoBack()) {
                    myWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);

OnBackPressed usulida

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

Fragment Impementation usuli,

if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.addToBackStack("close");
        ft.commit();




    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
0
Bordrolaringiz elementini bosganingizda fragmentlaringizni ni qanday qo'shishingizga aniqroq aytib bera olasizmi?
qo'shib qo'ydi muallif himanshu1496, manba
Bizning qismlaringizni amalga oshirishingizni ko'rsating.
qo'shib qo'ydi muallif FiN, manba
agar (fragment! = null) {FragmentTransaction ft = getSupportFragmentManager (). beginTransaction (); ft.replace (R.id.content_main, fragment); ft.commit (); } DrawerLayout drawer = (DrawerLayout) findViewById (R.id.drawer_layout); drawer.closeDrawer (GravityCompat.START);
qo'shib qo'ydi muallif Shakti Patra, manba
agar (fragment! = null) {FragmentTransaction ft = getSupportFragmentManager (). beginTransaction (); ft.replace (R.id.content_main, fragment); ft.commit (); } DrawerLayout drawer = (DrawerLayout) findViewById (R.id.drawer_layout); drawer.closeDrawer (GravityCompat.START);
qo'shib qo'ydi muallif Shakti Patra, manba

8 javoblar

O'ylaymanki, parchalaringizni orqaga qaytarib olmaysiz.

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//your code
fragmentTransaction.addToBackStack("your_tag");
0
qo'shib qo'ydi

O'ylaymanki, parchalaringizni orqaga qaytarib olmaysiz.

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//your code
fragmentTransaction.addToBackStack("your_tag");
0
qo'shib qo'ydi

Buni quyidagi tarzda qilish kerak

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            //that means your stack is empty and you want to close activity
            finish();
        } else {
           //pop the backstack here
            getSupportFragmentManager().popBackStackImmediate();
        }
    }

}
0
qo'shib qo'ydi

Buni quyidagi tarzda qilish kerak

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            //that means your stack is empty and you want to close activity
            finish();
        } else {
           //pop the backstack here
            getSupportFragmentManager().popBackStackImmediate();
        }
    }

}
0
qo'shib qo'ydi

Biror qismni keltirganingizda, bu kabi to'plamga qo'shing

fragmentTransaction.addToBackStack("fragment");

va keyin onBackPress uni bu kabi suyakka pop

FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        fm.popBackStack ("fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
0
qo'shib qo'ydi

Biror qismni keltirganingizda, bu kabi to'plamga qo'shing

fragmentTransaction.addToBackStack("fragment");

va keyin onBackPress uni bu kabi suyakka pop

FragmentManager fm = getActivity()
                .getSupportFragmentManager();
        fm.popBackStack ("fragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
0
qo'shib qo'ydi

When you add a fragment to some container or inflate it using xml, it is not added to the backstack and so, when 'onBackPressed' occurs it throws the last activity from the backstack- which is the activity hosting the fragment and thus exiting the application. I think this post: Android: Fragments backStack

Sizga yordam berishi mumkin, parcha jarayonini orqaga qaytariladigan qismga qanday kiritish kerakligini tushuntiradi:

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(..............);
    fragmentTransaction.addToBackStack("fragment transaction name, not required");
    fragmentTransaction.commit(); 
0
qo'shib qo'ydi
U ishladi! U qanday ishlaydi? Lekin insonga rahmat
qo'shib qo'ydi muallif yahme3, manba
Salom uchun rahmat, lekin bu men uchun ish bermadi ... Men savolni parcha dastur bilan almashtirdim ...
qo'shib qo'ydi muallif Shakti Patra, manba

When you add a fragment to some container or inflate it using xml, it is not added to the backstack and so, when 'onBackPressed' occurs it throws the last activity from the backstack- which is the activity hosting the fragment and thus exiting the application. I think this post: Android: Fragments backStack

Sizga yordam berishi mumkin, parcha jarayonini orqaga qaytariladigan qismga qanday kiritish kerakligini tushuntiradi:

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(..............);
    fragmentTransaction.addToBackStack("fragment transaction name, not required");
    fragmentTransaction.commit(); 
0
qo'shib qo'ydi
U ishladi! U qanday ishlaydi? Lekin insonga rahmat
qo'shib qo'ydi muallif yahme3, manba
Salom uchun rahmat, lekin bu men uchun ish bermadi ... Men savolni parcha dastur bilan almashtirdim ...
qo'shib qo'ydi muallif Shakti Patra, manba