1

Everytime I rotate the phone´s screen, the adapter of a gridview gets reloaded.

I have this on my manifest (android:configChanges="orientation|keyboardHidden|keyboard|screenSize") and I know it´s working as it should: the oncreate() is not called(where the adapter is normally set) and the onConfigurationChanged is called(where I have just a simple toast for verification).

The problem is, I don´t know from where(method, activity,process, etc), the adapter is called, used, reset and reloaded to the gridview everytime I rotate phone.

I just need to find a way for the adapter to be set only in the onCreate(), when the activity is first created.. nothing should happen when the phone is rotated.... that´s all...

I use the same thing for other activities where there are not gridviews and it works perfectly, just using manifest´s code shown above....

Any help deeply appreciated...

2 Answers2

0

please have a try with add the "screenSize" attribute like I did below:

<activity
    android:name=".YourActivityName"
    android:configChanges="orientation|screenSize">
</activity>
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.http://developer.android.com/guide/topics/resources/runtime-changes.html – Raghunandan Jan 15 '14 at 06:24
  • yes!! please suggest what can be the possible solution to the question, it will help all of us for learning as well!! – Jitesh Upadhyay Jan 15 '14 at 06:29
  • solution is there in the link provided under the topic Retaining object configuration during orientation change – Raghunandan Jan 15 '14 at 06:30
  • Thanks Jitesh, already using that... still I changed it to just those 2, same results.. – Carlos Torrenegra Jan 15 '14 at 06:33
  • Raghunandan, please check your link, 404 error, file not found... thanks. – Carlos Torrenegra Jan 15 '14 at 06:36
  • the link is http://developer.android.com/guide/topics/resources/runtime-changes.html – Jitesh Upadhyay Jan 15 '14 at 06:40
  • @CarlosAndresTorrenegraE it does work for me. http://developer.android.com/guide/topics/resources/runtime-changes.html – Raghunandan Jan 15 '14 at 06:41
  • Raghunandan which solution do you suggest based on your link? Topic b.) is the approach I´m already using... so? for topic a.) what should I retain? what variable-object? for the gridview seems not so clear... could you expand a little, would be helpful.. thanks. – Carlos Torrenegra Jan 15 '14 at 17:40
0

I think the problem is with your Adapter class. Try to use this code:

Inside Activity onCreate method:

GridView grid = (GridView) findViewById(R.id.gridView);
grid.setAdapter(new Adapter(this));

Adapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends BaseAdapter {

private Context mContext;
private LayoutInflater mInflator;

public Adapter(Context context) {

    this.mContext = context;
    mInflator = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return 10;
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

static class ViewHolder {

    private TextView appLable;
    private ImageView appICon;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = null;
    if (convertView == null) {
        final ViewHolder vHolder = new ViewHolder();
        view = mInflator.inflate(R.layout.item, null);
        vHolder.appLable = (TextView) view.findViewById(R.id.lable);
        vHolder.appICon = (ImageView) view.findViewById(R.id.icon);
        view.setTag(vHolder);

    } else {
        view = convertView;

    }

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.appLable.setText("position " + position);

    return view;
}
}

Put this inside manifest activity tag:

android:configChanges="orientation" 

Hope it will help you:)

Sonali8890
  • 2,005
  • 12
  • 16