views:

279

answers:

2

EDIT: This problem may be related to http://stackoverflow.com/questions/3121153/baseadapter-causing-listview-to-go-out-of-order-when-scrolled


My main application screen uses a GridView to display a grid of icons. Each icon has an image, name, and Intent. I have the Activity set to handle orientation changes so I can change the background. The onCreate method sets up the GridView with 3 test icons that don't do anything and a preferences icon which opens a PreferencesActivity:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GridView gv = (GridView) findViewById(R.id.main_grid);
if (getResources().getConfiguration().orientation ==
        Configuration.ORIENTATION_LANDSCAPE)
    gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
    gv.setBackgroundResource(R.drawable.main_bg);

ArrayList<Icon> icons = new ArrayList<Icon>();
icons.add(new Icon("Test 1", R.drawable.test1, null));
icons.add(new Icon("Test 2", R.drawable.test2, null));
icons.add(new Icon("Test 3", R.drawable.test3, null));
icons.add(new Icon("Preferences", R.drawable.perferences, new Intent(this,
        AlertPreferences.class)));
IconAdapter ia = new IconAdapter(icons);
gv.setAdapter(ia);
gv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
        Intent i = ((IconAdapter)arg0.getAdapter()).getItem(arg2).getIntent();
        if (i != null)
            HomeActivity.this.startActivity(i);
    }
});

And the onConfigurationChanged is very simple:

super.onConfigurationChanged(newConfig);    
GridView gv = (GridView) findViewById(R.id.main_grid);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
    gv.setBackgroundResource(R.drawable.main_bg);

The background changes just fine but the code introduces a new problem with the GridView. Whatever orientation I start the application in it works just fine. However, when I change orientation, the order of the icons in the GridView reverses. And when I rotate the screen back to the orientation it started in, everything goes back to normal. What's going on here?

A: 

Try to separate the logic where you are setting up the user interface:

GridView gv;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    configureYourUIHere();    
}

public void configureYourUIHere(){
    gv = (GridView) findViewById(R.id.main_grid);
    if (getResources().getConfiguration().orientation ==
            Configuration.ORIENTATION_LANDSCAPE)
        gv.setBackgroundResource(R.drawable.main_bg_horiz);
    else
        gv.setBackgroundResource(R.drawable.main_bg);

    ArrayList<Icon> icons = new ArrayList<Icon>();
    icons.add(new Icon("Test 1", R.drawable.test1, null));
    icons.add(new Icon("Test 2", R.drawable.test2, null));
    icons.add(new Icon("Test 3", R.drawable.test3, null));
    icons.add(new Icon("Preferences", R.drawable.perferences, new Intent(this,
            AlertPreferences.class)));
    IconAdapter ia = new IconAdapter(icons);
    gv.setAdapter(ia);
    gv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            Intent i = ((IconAdapter)arg0.getAdapter()).getItem(arg2).getIntent();
            if (i != null)
                HomeActivity.this.startActivity(i);
        }
    });
}

public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);    
    configureYourUIHere();
}
Cristian
Spoke too soon. I spent some more time testing and found that I can still cause the problem if I make press the preferences button (so the preferences activity comes up), rotate the screen, and then hit the back button. There must be a better fix than having to reinitialize everything anytime the screen is rotated or may have been rotated.
Mr. Ambiguous
Actually, Android calls the `onDestroy` and `onCreate` methods when the orientation changes, so you have to reinitialize the UI. In this very case what you can do is trying to handle that 'rotation' by your self, rather than letting the OS to do it. You can do so by adding this to the activity reference on the manifest: `android:configChanges="keyboardHidden|orientation"`
Cristian
I already have that line in my manifest. I also can't remove them and let it go to `onCreate` every time because although it doesn't matter in this specific activity, I have other similar ones where it does.
Mr. Ambiguous
A: 

It was in fact a problem with the custom Adapter. The problem and solution are both here http://stackoverflow.com/questions/3121153/baseadapter-causing-listview-to-go-out-of-order-when-scrolled

Mr. Ambiguous