0

The buttons don't work when I change into landscape mode and back to portrait. Anyways, here is my code:

public class fragmentone extends Fragment {
    Button biological;
    Button natural;
    View myView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.firstlay, container, false);
        biological = (Button) myView.findViewById(R.id.biological);
        biological.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BiologicalHazards fragment = new BiologicalHazards();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.beginTransaction().addToBackStack("hi")
                        .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                        .replace(R.id.container1, fragment)
                        .commit();
            }
        });
        natural = (Button) myView.findViewById(R.id.natural);
        natural.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaturalHazards fragment = new NaturalHazards();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.beginTransaction().addToBackStack("hi")
                        .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                        .replace(R.id.container1, fragment)
                        .commit();
            }
        });
        return myView;
    }
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            getActivity().setContentView(R.layout.firstlay);
        }else{
            getActivity().setContentView(R.layout.firstlay);
        }
    }
}

Does anyone know how to solve this? Any help is appreciated and thanks in advance.

GabEA
  • 19
  • 3

3 Answers3

0

you need to add setRetainInstance(true); //to your fragment onCreateView()

Also needn't be doing this getActivity().setContentView(R.layout.firstlay); in your onConfigurationChanged() method.

Hope this help :)

Priya Singhal
  • 1,261
  • 11
  • 16
  • I've already added `setRetainInstance(true)` and removed getActivity but the layout for the landscape doesn't work – GabEA Sep 19 '15 at 06:02
  • Is R.layout.firstlay u used in onConfigurationChanged() different from what you set in onCreate()? – Priya Singhal Sep 19 '15 at 06:04
  • the R.layout.firstlay in onConfigurationChanged has a different layout in landscape mode. The ones in onCreate() and in the else statement is the same – GabEA Sep 19 '15 at 06:08
  • In which layout folder you keep your land R.layout.firstlay ? Is it kept inside layout-land folder as explained in this link : http://stackoverflow.com/a/4858052/4079431 – Priya Singhal Sep 19 '15 at 06:12
  • the one for the landscape is in layout-land folder – GabEA Sep 19 '15 at 06:14
0

I think You to have re initilize when change orientation

try your modified code

private LayoutInflater inflater;
@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = inflater.inflate(R.layout.firstlay, container, false);
    reInitilize(myView);
    return myView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        myView = inflater.inflate(R.layout.firstlay, null);
        reInitilize(myView);
    }else{
        myView = inflater.inflate(R.layout.firstlay, null);
        reInitilize(myView);
    }
}
private void reInitilize(View myView) {
    // TODO Auto-generated method stub
    biological = (Button) myView.findViewById(R.id.biological);
    biological.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BiologicalHazards fragment = new BiologicalHazards();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction().addToBackStack("hi")
                    .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                    .replace(R.id.container1, fragment)
                    .commit();
        }
    });
    natural = (Button) myView.findViewById(R.id.natural);
    natural.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NaturalHazards fragment = new NaturalHazards();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction().addToBackStack("hi")
                    .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                    .replace(R.id.container1, fragment)
                    .commit();
        }
    });

}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
  • I'm getting an error that says: cannot resolve symbol inflater – GabEA Sep 19 '15 at 06:51
  • add `inflater = LayoutInflater.from(context);` or `inflater = LayoutInflater.from(getActivity());` inside `onConfigurationChanged` – Kishore Jethava Sep 19 '15 at 06:54
  • I'm getting a nullpointerException error pointing at, I'm guessing, in the inflater at `myView = inflater.inflate(R.layout.first0, null);` – GabEA Sep 19 '15 at 07:09
  • I've added everything and even copy/pasted your code. Still it doesn't work. – GabEA Sep 19 '15 at 07:33
0

You don't need to do handle orientation your self just to change your layout. Create 2 files with this same name R.layout.fistlay. One in res/layout, one in res/layout-land. Android will automatically pick the right one base on the orientation.

you could implement the method

@Override
protected void onSaveInstanceState(Bundle bundle){
    // store some variable like boolean, ints, String to the bundle
    bundle.putBoolean('currentFragment', isInFragmentA);
    // call the default method
    super.onSaveInstanceState(bundle);
}

After this, you could check if you are on a configuration change as following:

void onCreate(Bundle bundle){
    if (bundle == null){
       //start the activity as the use is opening the app as normal
    } else {
        // bundle is not null
        //get back the variable you have store and act accordingly
        // e.g do some FragmentTransition 

    }
}

Hope this help

Tin Tran
  • 2,265
  • 1
  • 14
  • 17
  • Now the problem with this is that when I initiate a new fragment, I go back to the first fragment when I change orientation. I wanted to stay on the fragment currently initiated. – GabEA Sep 19 '15 at 10:13
  • Could you explain further a little bit ? :) – Tin Tran Sep 19 '15 at 10:39
  • Example: Fragments A and B. I go to Fragment B from A but when the orientation changes, I go back to fragment A. I need to stay on B. – GabEA Sep 19 '15 at 11:04
  • You are very active :) – Tin Tran Sep 19 '15 at 12:49