我是android的初学者,我用fragment创建了应用程序。当我旋转屏幕(在fragment屏幕时)它离开那个fragment屏幕并跳转到主活动屏幕。为什么?请帮我解决这个问题。
我的主要活动, Individualuser_Safmical.java:
public class IndividualUser_Safmical extends AppCompatActivity
{
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
fm = getFragmentManager();
super.onCreate(savedInstanceState);
setContentView(R.layout.i_activity_safmical);
}
public void shopbycategory(View v) {
fm.beginTransaction().replace(R.id.content_frame, new ShopFragment()).addToBackStack("shop").commit();
}
}
我的片段ShopFrgment.java:
public class ShopFragment extends Fragment {
FragmentManager fm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.i_fragment_shop,container,false);
String[] list1={"Chemicals","Pigments","Uniforms","Safety Products"};
ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
listView.setAdapter(listviewadapter);
fm =getFragmentManager();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
fm.beginTransaction().replace(R.id.content_frame,new Chemical_Fragment()).addToBackStack("shop").commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new Pigments_Fragment()).addToBackStack("shop").commit();
}
if(position==2){
fm.beginTransaction().replace(R.id.content_frame,new Uniforms_Fragment()).addToBackStack("shop").commit();
}
if(position==3){
fm.beginTransaction().replace(R.id.content_frame,new SafetyProducts_Fragment()).addToBackStack("shop").commit();
}
}
});
return rootview;
}
}