当我按下主页按钮时,我的片段中的数据正在保存,但是当我切换到另一个片段时,甚至没有调用该方法。
我什至尝试将 setRetainInstance() 设置为 false,但它仍然没有调用该函数。
RedFragment.java
import com.actionbarsherlock.app.SherlockFragment;
import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
EditText[] etCollection;
EditText mText;
int R1C1, R1C2;
String R1C3="";
View tView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
//super.onCreate(savedInstanceState);
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) mView.findViewById(R.id.gridViewRed);
setRetainInstance(false);//Do I need this?
tView = container.getRootView();
/*if(savedInstanceState != null)
{
//R1C1 = savedInstanceState.getInt("R1C1");
R1C2 = savedInstanceState.getInt("R1C2");
R1C3 = savedInstanceState.getString("R1C3");
//Log.d("savedd R1C1 ", String.valueOf(R1C1));
Log.d("savedd R1C2 ", String.valueOf(R1C2));
Log.d("savedd R1C3 ", String.valueOf(R1C3));
}
*/
mGrid.setAdapter(new ImageAdapter(getActivity()));
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
//R1C1 = savedInstanceState.getInt("R1C1", 0);
R1C2 = savedInstanceState.getInt("R1C2", 0);
R1C3 = savedInstanceState.getString("R1C3");
//Log.v("saved R1C1 ", String.valueOf(R1C1));
Log.v("saved R1C2 ", String.valueOf(R1C2));
Log.v("saved R1C3 ", R1C3);
}
else
Log.w("instance state", "The instance state was null");
//Log.v("saved R1C1 ", String.valueOf(R1C1));
//Log.v("saved R1C2 ", String.valueOf(R1C2));
//Log.v("saved R1C3 ", String.valueOf(R1C3));
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("R1C2", Integer.valueOf(etCollection[4].getText().toString()));
Log.v("Saving", etCollection[0].getText().toString());
savedInstanceState.putString("R1C3", etCollection[4].getText().toString());
}
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
etCollection = new EditText[getCount()];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
//LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
LayoutInflater li = infl;
myView= li.inflate(R.layout.grid_items, null);
}
EditText editText = (EditText) myView.findViewById(R.id.grid_item_edit_text_red);
etCollection[position] = editText;
EditText oText = (EditText)tView.findViewById(R.id.grid_item_edit_text_red);
etCollection[0] = oText;
return myView;
}
@Override
public int getCount() {
return 9;//only 9
}
@Override
public Object getItem(int position) {
Log.w("etCollection: " + position , etCollection[position].getText().toString() );
return etCollection[position];
}
@Override
public long getItemId(int position) {
return 0;//mGrid.getItemIdAtPosition(position);
}
}
}