我目前正在开发一个Android应用程序,它使用带有片段的导航抽屉,每个片段都包含ImageButtons。用户可以更改这些ImageButtons的背景图像。
但是,当我进行更改时,切换到另一个片段,然后切换回初始片段,重置更改(ImageButton背景重置为默认值)。
我知道我的当前代码每次选中时都会重新创建片段;我怎样才能在离开它们的状态下重新打开碎片?
PS。即使应用程序关闭并重新打开,我也可以保存这些更改的解决方案。谢谢!
这是我的主抽屉活动方法:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_agni) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentAgni()).commit();
} else if (id == R.id.nav_ah_muzen_cab) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentAhMuzenCab()).commit();
} else if (id == R.id.nav_ah_puch) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentAhPuch()).commit();
} else if (id == R.id.nav_amaterasu) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new FragmentAmaterasu()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
这是我的一个片段(它们几乎都是一样的):
public class FragmentAhMuzenCab extends Fragment {
View myView;
public FragmentAhMuzenCab() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.god_ah_muzen_cab, container, false);
//ImageButtons
//Starter Item + Relics
final ImageButton starter = (ImageButton) myView.findViewById(R.id.imageStarterItem);
final ImageButton relic1 = (ImageButton) myView.findViewById(R.id.imageRelic1);
final ImageButton relic2 = (ImageButton) myView.findViewById(R.id.imageRelic2);
//Full Build
final ImageButton item1 = (ImageButton) myView.findViewById(R.id.imageItem1);
final ImageButton item2 = (ImageButton) myView.findViewById(R.id.imageItem2);
final ImageButton item3 = (ImageButton) myView.findViewById(R.id.imageItem3);
final ImageButton item4 = (ImageButton) myView.findViewById(R.id.imageItem4);
final ImageButton item5 = (ImageButton) myView.findViewById(R.id.imageItem5);
final ImageButton item6 = (ImageButton) myView.findViewById(R.id.imageItem6);
starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysStarterFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
starter.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "starterDialog");
}
});
relic1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemRelicFragment dialog = ItemRelicFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemRelicFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
relic1.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "relic1Dialog");
}
});
relic2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemRelicFragment dialog = ItemRelicFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemRelicFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
relic2.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "relic2Dialog");
}
});
item1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item1.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item1Dialog");
}
});
item2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item2.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item2Dialog");
}
});
item3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item3.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item3Dialog");
}
});
item4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item4.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item4Dialog");
}
});
item5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item5.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item5Dialog");
}
});
item6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysFragment dialog = ItemPhysFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
item6.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "item6Dialog");
}
});
return myView;
}
}
答案 0 :(得分:1)
是的,片段被重新创建,我建议你在SharedPreferences中存储图像的资源ID:
starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.setOnListItemSelectedListener(new ItemPhysStarterFragment.OnListItemClickedListener() {
public void onListItemClick(int resourceId) {
...store the resourceId for this image in the Shared Preferences
starter.setImageResource(resourceId);
}
});
dialog.show(getFragmentManager(), "starterDialog");
}
});
然后在你的Fragment's onCreate中你可以做类似的事情:
starter.setImageResource(...get resource id from shared prefs);