qanday qismni boshqa qism deb atashni qanday bilish mumkin?

Androidda bu muammo bor. A , B , C uch qismini ko'rib chiqing. C kodini A yoki B dan olish mumkin. C deb nomlangan qaysi qismdan bilish uchun baribir bormi?

tahrirlangan

Ok bolalar, men nima qilishni istayotganimni tushuntirmoqchi edim. Aytaylik, menda ushbu chaqiruv mavjud: B, B qo'ng'iroqlari.

Menda B ni qaytarish tugmachasini bosganimda, bu yaxshi. Lekin yana tugmachani bosganimda, meni A o'rniga C ga olib boradi.

Bu mening kodim:

@Override
public void onBackPressed() { 

    //this is the current fragment
    Fragment fragmentActual =     this.getSupportFragmentManager().findFragmentById(android.R.id.tabcontent);

    String fragmentTag = fragmentoActual.getTag().toString();

            //If I press the back button in C:
    if(fragmentTag.equals("TAG C")){

        Fragment removefragment = this.getSupportFragmentManager().findFragmentByTag("TAG B");
        Fragment fragmentClient = this.getSupportFragmentManager().findFragmentByTag("TAG C");

        //If Im NOT passing arguments to B, I know this is a new form
        if(removefragment.getArguments()== null){

                            //I always pass arguments to fragment C, but just in case:
            if(fragmentClient.getArguments()!= null){

                Bundle mArguments = fragmentClient.getArguments();

                                    //FRAGMENT B
                FragmentB  fragmentB = new FragmentB ();
                fragment.setArguments(mArguments);

                FragmentManager manager = this.getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();


                ft.addToBackStack(null);
                    ft.replace(android.R.id.tabcontent,fragmentB,"TAG B");
                    ft.commit();
                }
        }else{

            super.onBackPressed();
        }

    }else{
        super.onBackPressed();
    }

}   

Endi kodni tushuntiraman. Asosan, B fragmentini almashtirish uchun C fragmentini chaqirish kerak. Buni qilaman, chunki men B fragmaniga argumentlarni kiritishim kerak. Ammo almashtirishda B fragmentining "tarix" yo'qoladi, men B ni qaytarish tugmasini bosganimda, men A < strong> (BILAN QANDAY QABUL QILIShIM BO'LSA QANDAY BILIB BORMI) .

B fragmentini chaqirganimda, men bo'sh joyga ega bo'lgan argumentlarni qabul qilmayman. Lekin men C ga qo'ng'iroq qilsam, Vda qolsam, B fragmentiga (qaytarish tugmasi bosilganda) argumentlarni kiritishim kerak, shuning uchun yangilangan ma'lumotni ko'rsatishi mumkin.

Iltimos, aniq bo'lmagan biror narsa bo'lsa, menga bildiring, men o'zimni yaxshiroq tushuntira olaman.

Edited 2: This issue has something with this https://stackoverflow.com/questions/16703604/back-press-button-when-i-save-a-form-for-the-first-time-a-list-view-is-not-updat. Maybe it does my idea more clear.

1
Bu haqda ko'proq ma'lumot bera olasizmi?
qo'shib qo'ydi muallif kiduxa, manba
Bu haqda ko'proq ma'lumot bera olasizmi?
qo'shib qo'ydi muallif kiduxa, manba
Mening vaziyatimda bunga ehtiyoj bor, chunki mendaBackButtonPressed muammolar bor. Bilishim kerakki, kimga qo'ng'iroq qilsam, orqaga qarab tugma bosilganda, o'zimni qaytarib olaman.
qo'shib qo'ydi muallif kiduxa, manba
Mening vaziyatimda bunga ehtiyoj bor, chunki mendaBackButtonPressed muammolar bor. Bilishim kerakki, kimga qo'ng'iroq qilsam, orqaga qarab tugma bosilganda, o'zimni qaytarib olaman.
qo'shib qo'ydi muallif kiduxa, manba
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba
Androidning introspection qobiliyatini izlayapsiz.
qo'shib qo'ydi muallif fotanus, manba
Bir parcha boshqa "parcha" deb nomlangan qismni bilishni talab qilmasligi kerak.
qo'shib qo'ydi muallif Luksprog, manba
Bir parcha boshqa "parcha" deb nomlangan qismni bilishni talab qilmasligi kerak.
qo'shib qo'ydi muallif Luksprog, manba
Siz ushbu muammolarni hal qilishingiz mumkin. Bundle ma'lumotlarni yangi yaratilgan fragmentga kimlar chaqirganligini eslatib o'tishni har doim tanlay olasiz. Keyin fragmentda getArgument() dan foydalanib, kim uni chaqirganini bilib oladi.
qo'shib qo'ydi muallif Luksprog, manba
Siz ushbu muammolarni hal qilishingiz mumkin. Bundle ma'lumotlarni yangi yaratilgan fragmentga kimlar chaqirganligini eslatib o'tishni har doim tanlay olasiz. Keyin fragmentda getArgument() dan foydalanib, kim uni chaqirganini bilib oladi.
qo'shib qo'ydi muallif Luksprog, manba

8 javoblar

Luksprogning fikri eng yaxshi deb o'ylayman: "Siz bu masalalarni hal qilishni xohlashingiz mumkin, siz har doim yangi to'plamga chaqirgan kishilarning ma'lumoti bilan ma'lumot to'plami bilan o'tishni tanlaysiz, keyin fragmentda getArgument() ni bilib olasiz uni chaqirgan ".

Yana yaxshi bir yo'l topmadim.

3
qo'shib qo'ydi

Luksprogning fikri eng yaxshi deb o'ylayman: "Siz bu masalalarni hal qilishni xohlashingiz mumkin, siz har doim yangi to'plamga chaqirgan kishilarning ma'lumoti bilan ma'lumot to'plami bilan o'tishni tanlaysiz, keyin fragmentda getArgument() ni bilib olasiz uni chaqirgan ".

Yana yaxshi bir yo'l topmadim.

3
qo'shib qo'ydi

You can use the setTargetFragment method to set which was the parent fragment. Then you can use the method getTargetFragment to find out who called you.

1
qo'shib qo'ydi

You can use the setTargetFragment method to set which was the parent fragment. Then you can use the method getTargetFragment to find out who called you.

1
qo'shib qo'ydi

You can use FragmentManager for creating a back stack of your fragments. You also have to work with Fragment Transactions first. Further informations see: http://developer.android.com/guide/components/fragments.html#Transactions

0
qo'shib qo'ydi
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba

You can use FragmentManager for creating a back stack of your fragments. You also have to work with Fragment Transactions first. Further informations see: http://developer.android.com/guide/components/fragments.html#Transactions

0
qo'shib qo'ydi
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba

Siz nima qilayotganingizni FragmentTransaction qismi sifatida addToBackStack() ga murojaat qiling:

Menimcha, bu sizga kerak.

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}

@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.allowBackPressed()) {//and then you define a method allowBackPressed with the logic to allow back pressed or not
        super.onBackPressed();
    }
}
0
qo'shib qo'ydi
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba

Siz nima qilayotganingizni FragmentTransaction qismi sifatida addToBackStack() ga murojaat qiling:

Menimcha, bu sizga kerak.

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}

@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.allowBackPressed()) {//and then you define a method allowBackPressed with the logic to allow back pressed or not
        super.onBackPressed();
    }
}
0
qo'shib qo'ydi
Men tahrirlangan savolni ko'rib chiqing
qo'shib qo'ydi muallif kiduxa, manba