-1

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;
    }
}
dev
  • 69
  • 8

3 Answers3

3

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"
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • do i need to write onConfigurationchanged method in fragment? – dev May 25 '17 at 07:12
  • Add in activity, and check if configuration changed then again call fragment – Abdul Kawee May 25 '17 at 07:38
  • But this fragment is called when button "shop by category" clicked.Then where should i add this code in my activity? – dev May 25 '17 at 08:12
  • its an `OverRide` method like `onCreate` or `onDestroy` so add it any where in your activity, its auto called when configuration change occurs – Abdul Kawee May 25 '17 at 08:14
  • minimal solution is to lock your application in portrait mode so that it cannot rotate and your problem is solved – Abdul Kawee May 25 '17 at 08:15
1

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

Hasan Saykın
  • 138
  • 1
  • 8
0

@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.

Abbas
  • 3,529
  • 5
  • 36
  • 64