我正在使用 ViewPager 并有 3 个片段。
我知道第一次加载 ViewPager 时默认加载所有片段,
最小值为 3 个片段,viewpager 执行片段的所有生命周期方法。
我注意到的问题:
每当我滑动 viewpager 时,所选片段不会再次调用其任何生命周期方法。
因此我无法直接访问全局变量。
例如:如果我在 OnCreateView() 中初始化了一个首选项,当我尝试通过初始化该片段的实例并调用该片段中的方法从活动中访问它时,我得到了 NPE。
在第一次加载后,甚至不会调用任何片段的 onResume。
我想知道的:
在 onCreateView() 中初始化视图和首选项后,如何访问它们?
但是对于在 onCreateView() 中初始化的按钮,单击它会调用 Web 服务并按照我的意愿完美运行。如何?
在过去的 3 天里,我一直在解决这些问题,并用谷歌搜索了很多,但没有找到我的答案。
任何帮助将不胜感激。
片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_dth, container, false);
preferences=
PreferenceManager.getDefaultSharedPreferences(getActivity());
editor = preferences.edit();
editAmt = (EditText) v.findViewById(R.id.editAmt);
browsplan = (Button) v.findViewById(R.id.browsplan);
Log.e("DTH onCreateView ",""+page);
String fontpath = "fonts/OpenSans-Regular.ttf";
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
fontpath);
editCustomerid.setTypeface(tf);
editAmt.setTypeface(tf);
token=preferences.getString("Token","-1");
mobileNo=preferences.getString("userMobileNo","0");
browsplan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//new DTHOperator(getActivity()).execute(); HERE IT WORKS PROPERLY
}
});
return v;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && isResumed()){
new DTHOperator(getActivity()).execute();
Toast.makeText(getActivity(),"setUserVisibleHint DTH "
,Toast.LENGTH_SHORT).show();
}
}
public class DTHOperator extends AsyncTask<Context, Integer, String> {
Context ctx;
DTHOperator(Context ctx) {
this.ctx = ctx;
}
@Override
protected String doInBackground(Context... params) {
List<NameValuePair> telecomdata = new ArrayList<NameValuePair>();
String result;
Log.d("Register Activity Token", /*preferences.getString("Token", "")*/token);
telecomdata.add(new BasicNameValuePair("mobNo", /*preferences.getString("userMobileNo", "")*/mobileNo));
telecomdata.add(new BasicNameValuePair("requestDate", Utilities.getApiCallTimeFormat()));
telecomdata.add(new BasicNameValuePair("reqFrom", "APP"));
Log.v("PostParameters", "telecomdata" + telecomdata);
if (Connectivity.checkNetwork(ctx)) {
result = rechargeUrl.queryRESTurlONline(ctx, "/GetDTHOperatorsService", "post", telecomdata, GenericConstants.PiPay_root);
} else {
result = GenericConstants.NETWORKNOTFOUND;
displayToast("The Internet Connection appears to be offline");
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
MyProgress.show(ctx, "", "");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
if (result != null) {
if (result.equalsIgnoreCase(GenericConstants.NETWORKNOTFOUND)) {
MyProgress.CancelDialog();
Toast.makeText(ctx,"The Internet Connection appears to be offline",Toast.LENGTH_SHORT).show();
return;
}
String[] resArr = result.split("delimiter_");
if (!resArr[0].equals("500")) {
MyProgress.CancelDialog();
Log.d("DTHOperator Response ::", result);
JSONArray jsonArray = null;
JSONObject object = new JSONObject(result);
jsonArray = object.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
if (jsonArray.getJSONObject(i).has("opName")) {
operator.add(jsonArray.getJSONObject(i).getString("opName"));
Log.d("", " Question size " + operator.size());
}
}
if(MyProgress.isShowingProgress())
MyProgress.CancelDialog();
editAmt.setText("2000"); // NPE HERE Also setting the value
} else {
MyProgress.CancelDialog();
displayToast("Failure");
}
}
} catch (Exception e) {
e.printStackTrace();
MyProgress.CancelDialog();
}finally {
if(MyProgress.isShowingProgress())
MyProgress.CancelDialog();
}
}