Whenever i rotate the screen it jumps to main activity screen from fragment screen

254 Views Asked by At

I am beginner in android,I created application with fragment.In which when i rotate the screen(when in fragment screen) it leaves that fragment screen and jumps to main activity screen.Why?Please help me to solve this.

My Main activity, 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();

    }
}

My fragment,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;
    }
}
3

There are 3 best solutions below

0
On

Check out saving activity state and restoring it again examples. You must apply your solution there. Here is the official documentation. https://developer.android.com/guide/topics/resources/runtime-changes.html

5
On

You can use onConfigurationChanged

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

}

Also add this line your manifest

android:configChanges="keyboardHidden|orientation|screenSize"

OR

you can lock your activity to avoid orientation changes by adding this in your manifest

android:screenOrientation="portrait"
0
On

@Abdul Kawee wrote

Its because on rotating screen activity gets destroyed and recreated

He is right ofcourse. There are two ways you can handle this:

  1. Either Override your Fragment's onConfigurationChanged(), onSaveInstanceState() and onRestoreInstanceState() callback methods.
  2. Or you could add android:configChanges="keyboardHidden|orientation"in your app's manifest file.

For more detail see this SO Answer.