-1

我是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;
    }
}
4

3 回答 3

3

您可以使用onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

}

还要将此行添加到您的清单

android:configChanges="keyboardHidden|orientation|screenSize"

或者

您可以通过在清单中添加它来锁定您的活动以避免方向更改

android:screenOrientation="portrait"
于 2017-05-25T06:29:05.103 回答
1

查看保存活动状态并再次恢复它的示例。您必须在那里应用您的解决方案。这是官方文档。https://developer.android.com/guide/topics/resources/runtime-changes.html

于 2017-05-25T06:25:19.913 回答
0

@Abdul Kawee 写道

这是因为在旋转屏幕上活动被破坏并重新创建

他当然是对的。有两种方法可以处理此问题:

  1. OverrideFragmentonConfigurationChanged(),onSaveInstanceState()和回调onRestoreInstanceState()方法。
  2. 或者,您可以添加android:configChanges="keyboardHidden|orientation"应用的清单文件。

有关更多详细信息,请参阅此SO Answer

于 2017-05-25T06:33:13.003 回答