2

我有这个片段活动,它在其操作栏中使用下拉导航列表。根据选择的选项,它要么显示来自服务器的元素列表,要么调用其他活动(到目前为止,它显示了“其他活动”的面包块)。

事情是,我发现如果我选择说选项 2(第一个选项是 0),等待列表加载然后旋转设备,导航列表返回到位置 0(并加载了 0 选项列表)。我必须保存该选项,以便在旋转设备时,它仍会显示我选择的列表,但无论如何导航列表都会返回选项 0。如果我现在重新旋转设备,它会加载 0 选项列表(因为它是当时选择的一个)。

我的课从这个延伸

public class TapabookListNavigationalActivity extends TapabookBaseActivity 
implements OnNavigationListener {

protected Boolean  esUnTablet = true;
protected Fragment fragment1; 
protected Fragment fragment2;

protected ActionBar bar;
protected ActionBarDropDownAdapter dda;

public static final int ID_TAPAS_RECIENTES = 0;
public static final int ID_BUSCA_TAPA_MUNICIPIO = 1;
public static final int ID_BUSCA_TAPA_PROVINCIA = 2;
public static final int ID_BUSCA_TAPA= 3;
public static final int ID_BUSCA_BAR = 4;
public static final int ID_SUBE_TAPA= 5;

protected ArrayList<ActionBarMenuItem> opciones;
protected Dao mDao;
//protected static int ID_NAVIGATIONAL_THIS = 0;

public Dao getDao(){
    return mDao;
}

@Override
protected void checkUserToken() {
    if(ConfiguracionApp.getInstance().getTokenUsuario().equalsIgnoreCase("")){
        // no hay token
        Intent intent = new Intent(this, EntryFA.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getSupportMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);

    // Calling super after populating the menu is necessary here to ensure that the
    // action bar helpers have a chance to handle this event.
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // si no está logado, que vuelva al login
    checkUserToken();

    bar = getSupportActionBar();
    loadNavigationList(Tapabook.getProvincia(),Tapabook.getMunicipio());





}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    boolean handled = true;
    Tapabook.d("TapabookListNavigationalActivity onNavigationItemSelected : " + itemId);
    Intent intent = new Intent(this, TapaListFA.class);
    if(itemPosition == ID_TAPAS_RECIENTES || 
            itemPosition == ID_BUSCA_TAPA_MUNICIPIO || 
            itemPosition == ID_BUSCA_TAPA_PROVINCIA ||
            itemPosition == ID_BUSCA_TAPA){
        croutonInfo("ID_TAPAS_RECIENTES");
        intent.putExtra(TapaListFA.TYPE_LIST,ID_TAPAS_RECIENTES );
    }else if(itemPosition == ID_BUSCA_BAR ){
        croutonInfo("ID_BUSCA_BAR");
    }else if(itemPosition == ID_SUBE_TAPA ){
        croutonInfo("ID_SUBE_TAPA");
    }else{
        handled = false;
    }

    return handled;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    if (itemId == R.id.menu_item_acerca_de){

    }else if (itemId == R.id.menu_item_enviar_email){
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse(Tapabook.MAILTO));
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, Tapabook.SUBJECT);
        startActivity(intent);
    }else if (itemId == R.id.menu_item_privacidad){
        FragmentManager fm = getSupportFragmentManager();
        PrivacyDF pdf = new PrivacyDF();

        pdf.show(fm, "fragment_privacy");

    }
    return false;
}

@Override
public void onGeolocalizador(String direccion, String provincia,
        String municipio, String region, float precisionMetros) {
    super.onGeolocalizador(direccion, provincia, municipio, region, precisionMetros);
    loadNavigationList(provincia,municipio);

}
protected void loadNavigationList(String provincia,String municipio){
    opciones = new ArrayList<ActionBarMenuItem>();
    opciones.add(
            new ActionBarMenuItem(
                    ID_TAPAS_RECIENTES, 
                    //R.drawable.ic_menu_buscar_bar,
                    getString(R.string.menu_nav_ver_últimas_tapas)
                    ));
    opciones.add(
            new ActionBarMenuItem(
                    ID_BUSCA_TAPA_MUNICIPIO, 
                    //R.drawable.ic_menu_buscar_tapa,
                    getString(R.string.menu_nav_ver_tapas_municipio) 
                    + " " + municipio
                    ));
    Tapabook.d("ActionBarMenuItem Municipio "  + municipio);
    opciones.add(
            new ActionBarMenuItem(
                    ID_BUSCA_TAPA_PROVINCIA, 
                    //R.drawable.ic_menu_buscar_tapa,
                    getString(R.string.menu_nav_ver_tapas_provincia).replace("%%%", provincia) 
                    //+ " " + provincia
                    ));
    Tapabook.d("ActionBarMenuItem Provincia "  + provincia);
    opciones.add(
            new ActionBarMenuItem(
                    ID_BUSCA_TAPA,
                    //R.drawable.ic_menu_buscar_tapa, 
                    getString(R.string.menu_nav_buscar_tapas)
                    ));
    opciones.add(
            new ActionBarMenuItem(
                    ID_BUSCA_BAR,
                    //R.drawable.ic_menu_subir_tapa,
                    getString(R.string.menu_nav_buscar_bares)
                    ));
    opciones.add(
            new ActionBarMenuItem(
                    ID_SUBE_TAPA,
                    //R.drawable.ic_menu_subir_tapa,
                    getString(R.string.menu_nav_subir_tapa)
                    ));

    dda 
    = new ActionBarDropDownAdapter(
            this, 
            R.layout.actionbar_menu_item, 
            opciones);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    bar.setListNavigationCallbacks(dda, this);
}

}

这是显示列表的类

public class TapaListFA extends TapabookListNavigationalActivity {

public static final String TYPE_LIST ="list_type";
public static final String LIST_LOADED  ="list_loaded";
public static final String NAVIGATION_POINT ="navigation_point";
private static boolean loadedList = false;
private static int navigationPoint = -1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tapa_list);

    Bundle extras= getIntent().getExtras();
    int listType = extras.getInt(TYPE_LIST);
    //
    Tapabook.d("pre-prepareList >> " + listType);
    if(!loadedList){
        Tapabook.d("TapaListFA.onCreate loadedList not loaded, load list");
        if(navigationPoint>=0){
            Tapabook.d("TapaListFA.onCreate navPoint: " + navigationPoint);
            setNavigationPoint(navigationPoint);
            //prepareList(navigationPoint);
        }else{
            Tapabook.d("TapaListFA.onCreate listType: " + listType);
            setNavigationPoint(listType);
            //prepareList(listType);
        }
        loadedList = true;
    }


    Tapabook.d("TapaListFA.onCreate fin");
}



public void prepareList(int type){
    Tapabook.d("TapaListFA.prepareList: " + type);
    ListaTapasParametros ltparam = new ListaTapasParametros();
    ltparam.setPagina(0);
    switch (type){
    case ID_BUSCA_TAPA_MUNICIPIO:
        ltparam.setProvincia(Tapabook.getProvincia());
        break;
    case ID_BUSCA_TAPA_PROVINCIA:
        ltparam.setProvincia(Tapabook.getMunicipio());
        break;
    case ID_BUSCA_TAPA:
        croutonInfo("Buscador");
        break;
    case ID_TAPAS_RECIENTES:
        break;
    default:
        Tapabook.wtf("¿qué tipo está entrando?  " + type);
    }
    Tapabook.d("TapaListFA.prepareList: showTapaList" + type);
    showTapaList(type, ltparam);
}

private void showTapaList(int typeOfList, ListaTapasParametros params) {
    Tapabook.d("TapaListFA.showTapaList" + typeOfList);
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    TapaListFragment tlf = new TapaListFragment();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    ft.replace(R.id.tla_container, tlf);
    ft.addToBackStack(null);
    Bundle args = new Bundle();
    args.putInt(TYPE_LIST, typeOfList);
    // Commit the transaction
    tlf.setRetainInstance(true);
    ft.commitAllowingStateLoss();
    tlf.setArguments(args);
    tlf.loadTapaList(typeOfList, params, 0);
    Tapabook.d("TapaListFA.showTapaList fin " + typeOfList);
}

public void startSearch(ListaTapasParametros params){
    showTapaList(ID_BUSCA_TAPA, params);
}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    boolean handled = true;
    Intent intent;
    Tapabook.d("TapaListFA.onNavigationItemSelected : " + itemId);
    if(itemId == ID_TAPAS_RECIENTES  || itemId == ID_BUSCA_TAPA_MUNICIPIO 
            || itemId == ID_BUSCA_TAPA_PROVINCIA ){
        if(navigationPoint >= itemPosition){
            Tapabook.d("TapaListFA.onNavigationItemSelected prepareList navigationPoint : " + navigationPoint); 
            prepareList(navigationPoint );
        }else{
            Tapabook.d("TapaListFA.onNavigationItemSelected prepareList itemPosition: " + itemPosition);    
            prepareList(itemPosition);
        }

    }else if (itemId == ID_BUSCA_TAPA){
        FragmentManager fm = getSupportFragmentManager();
        TapaSearchDF tsdf = new TapaSearchDF();
        tsdf.setRetainInstance(true);
        tsdf.show(fm, "tapa_search_fragment");

    }else if(itemId == ID_BUSCA_BAR  || itemId == ID_SUBE_TAPA ){
        croutonInfo("ID_BUSCA_BAR/ID_SUBE_TAPA");
        Tapabook.d("TapaListFA.onNavigationItemSelected buscabar/subetapa : " + itemId);
        super.onNavigationItemSelected(itemPosition, itemId);
    }else{
        handled = false;
        Tapabook.d("TapaListFA.onNavigationItemSelected (unhandled) : " + itemId);
    }
    navigationPoint = (int) itemId;
    return handled;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Tapabook.d("TapaListFA.onSaveInstanceState loadedList=" + loadedList);
    outState.putBoolean(LIST_LOADED,loadedList);
    navigationPoint = bar.getSelectedNavigationIndex();
    Tapabook.d("TapaListFA.onRestoreInstanceState navigationPoint=" + navigationPoint);
    outState.putInt(NAVIGATION_POINT, navigationPoint);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    loadedList = savedInstanceState.getBoolean(LIST_LOADED);
    Tapabook.d("TapaListFA.onRestoreInstanceState loadedList=" + loadedList);
    navigationPoint = savedInstanceState.getInt(NAVIGATION_POINT);
    Tapabook.d("TapaListFA.onRestoreInstanceState navigationPoint=" + navigationPoint);
    setNavigationPoint(navigationPoint);

}


private void setNavigationPoint(int navPoint) {
    Tapabook.d("TapaListFA.setNavigationPoint navPoint: " + navPoint);
    if(navPoint>=0){
        bar.setSelectedNavigationItem(navPoint );
        navigationPoint = navPoint;
    }else
        Tapabook.w("TapaListFA.setNavigationPoint navPoint: " + navPoint);
}

}

这是活动开始时的LOGCAT

02-24 11:37:23.491: D/TAPABOOK(31943): activities.TapaListFA.onCreate
02-24 11:37:23.511: D/TAPABOOK(31943): TapaListFA.onCreate loadedList not loaded, load list
02-24 11:37:23.511: D/TAPABOOK(31943): TapaListFA.onCreate listType: 0
02-24 11:37:23.511: D/TAPABOOK(31943): TapaListFA.setNavigationPoint navPoint: 0
02-24 11:37:23.511: D/TAPABOOK(31943): TapaListFA.onCreate fin
02-24 11:37:23.511: D/TAPABOOK(31943): activities.TapaListFA.onStart
02-24 11:37:23.511: D/TAPABOOK(31943): activities.TapaListFA.onResume
02-24 11:37:24.182: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 0
02-24 11:37:24.182: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList navigationPoint : 0
02-24 11:37:24.182: D/TAPABOOK(31943): TapaListFA.prepareList: 0
02-24 11:37:24.182: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList0
02-24 11:37:24.182: D/TAPABOOK(31943): TapaListFA.showTapaList0
02-24 11:37:24.212: D/TAPABOOK(31943): TapaListFA.showTapaList fin 0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList navigationPoint : 0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.prepareList: 0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.showTapaList0
02-24 11:37:24.882: D/TAPABOOK(31943): TapaListFA.showTapaList fin 0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList navigationPoint : 0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.prepareList: 0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.showTapaList0
02-24 11:37:24.923: D/TAPABOOK(31943): TapaListFA.showTapaList fin 0

在这里,我在下拉列表中选择选项 1

02-24 11:38:04.234: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 1
02-24 11:38:04.234: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList itemPosition: 1
02-24 11:38:04.234: D/TAPABOOK(31943): TapaListFA.prepareList: 1
02-24 11:38:04.234: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList1
02-24 11:38:04.244: D/TAPABOOK(31943): TapaListFA.showTapaList1
02-24 11:38:04.244: D/TAPABOOK(31943): TapaListFA.showTapaList fin 1

现在我旋转设备。它显示了正确的列表,但下拉菜单返回选项 0

02-24 11:38:25.838: D/TAPABOOK(31943): activities.TapaListFA.onPause
02-24 11:38:25.838: D/TAPABOOK(31943): TapaListFA.onSaveInstanceState loadedList=true
02-24 11:38:25.838: D/TAPABOOK(31943): TapaListFA.onRestoreInstanceState navigationPoint=1
02-24 11:38:25.848: D/TAPABOOK(31943): activities.TapaListFA.onStop
02-24 11:38:25.888: D/TAPABOOK(31943): activities.TapaListFA.onCreate
02-24 11:38:25.948: D/TAPABOOK(31943): TapaListFA.onCreate fin
02-24 11:38:25.968: D/TAPABOOK(31943): activities.TapaListFA.onStart
02-24 11:38:25.968: D/TAPABOOK(31943): TapaListFA.onRestoreInstanceState loadedList=true
02-24 11:38:25.968: D/TAPABOOK(31943): TapaListFA.onRestoreInstanceState navigationPoint=1
02-24 11:38:25.968: D/TAPABOOK(31943): TapaListFA.setNavigationPoint navPoint: 1
02-24 11:38:25.968: D/TAPABOOK(31943): activities.TapaListFA.onResume
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 1
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList navigationPoint : 1
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.prepareList: 1
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList1
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.showTapaList1
02-24 11:38:26.028: D/TAPABOOK(31943): TapaListFA.showTapaList fin 1
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected : 0
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.onNavigationItemSelected prepareList navigationPoint : 1
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.prepareList: 1
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.prepareList: showTapaList1
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.showTapaList1
02-24 11:38:26.358: D/TAPABOOK(31943): TapaListFA.showTapaList fin 1

为什么它不断回到选项0?

4

1 回答 1

2

看看这个:

除非您另外指定,否则配置更改(例如屏幕方向、语言、输入设备等的更改)将导致您当前的活动被破坏,并通过正常的活动生命周期过程

在某些特殊情况下,您可能希望根据一种或多种类型的配置更改绕过重新启动活动。这是通过清单中的android:configChanges属性完成的。对于您说您在那里处理的任何类型的配置更改,您将收到对当前活动的 onConfigurationChanged(Configuration)方法的调用,而不是重新启动。但是,如果配置更改涉及您未处理的任何内容,则活动仍将重新启动,并且 不会调用onConfigurationChanged(Configuration) 。

于 2013-02-24T10:51:15.473 回答