アクションバーでドロップダウンナビゲーションリストを使用するこのフラグメントアクティビティがあります。選択したオプションに応じて、サーバーからの要素のリストを表示するか、他のアクティビティを呼び出します (これまでのところ、「他のアクティビティ」のクルトンが表示されています)。
つまり、オプション 2 (最初のオプションは 0) を選択し、リストが読み込まれるのを待ってからデバイスを回転させると、ナビゲーション リストが位置 0 に戻る (そして 0 オプション リストが読み込まれる) ことがわかりました。デバイスを回転させても、選択したリストが表示されるようにオプションを保存する必要がありますが、ナビゲーション リストは何があってもオプション 0 に戻ります。そして、デバイスを再度回転させると、0 オプション リストが読み込まれます (その時点で 1 つの選択であるため)。
私のクラスはこれから拡張されます
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 に戻り続けるのはなぜですか?