请查看以下代码
OutputFragment.java
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class OutputFragment extends Fragment implements AsyncResponse{
private ImageView backButton, resultOfferImage;
private EditText offerEdt, confidenceEdt, typeEdt;
JsonController j;
private View view;
private ProgressDialog progressDialog;
private DataSaver dataSaver;
private Bundle savedInstance;
private static int resultOfferImageInt = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Intializing instance variables
view = inflater.inflate(R.layout.output, container,false);
backButton = (ImageView)view.findViewById(R.id.back_button);
resultOfferImage = (ImageView)view.findViewById(R.id.result_image);
offerEdt = (EditText)view.findViewById(R.id.offer_edt);
confidenceEdt = (EditText)view.findViewById(R.id.confidence_edt);
typeEdt = (EditText)view.findViewById(R.id.type_of_user_edt);
dataSaver = DataSaver.getInstance();
if(savedInstanceState==null){
progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("Please Wait");
progressDialog.setMessage("Please Wait...Generating Results...");
progressDialog.show();
}
//Registering the Listeners
backButton.setOnClickListener(new BackButtonAction());
j = JsonController.getInstance();
j.delegate = OutputFragment.this;
return view;
}
//ActionListener for the manual back button
private class BackButtonAction implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
fm.popBackStackImmediate();
}
}
@Override
public void processFinish(String output) {
// TODO Auto-generated method stub
Log.d("OUTPUT_FRAGMENT", output);
try {
JSONObject finalResult = new JSONObject(output);
//Set the Confidence
JSONObject jb1 = finalResult.getJSONObject("prediction_path");
Double confidence = jb1.getDouble("confidence");
confidence = confidence*100;
confidenceEdt.setText(String.valueOf(Math.round(confidence*100)/100)+"%");
//Set the type of User
String user = finalResult.getString("output");
typeEdt.setText(user);
Log.d("DataSaver_getConfidence", ""+dataSaver.getConfidence());
if((dataSaver.getConfidence())>confidence)
{
offerEdt.setText("No");
resultOfferImage.setImageResource(R.drawable.no_message_required);
resultOfferImageInt = R.drawable.no_message_required;
}
else
{
if(user.toLowerCase().equals("whale"))
{
offerEdt.setText("Yes");
resultOfferImage.setImageResource(R.drawable.eventinier_whale);
resultOfferImageInt = R.drawable.eventinier_whale;
}
else if(user.toLowerCase().equals("fish"))
{
offerEdt.setText("Yes");
resultOfferImage.setImageResource(R.drawable.tiny_fish);
resultOfferImageInt = R.drawable.tiny_fish;
}
else if(user.toLowerCase().equals("minnow"))
{
offerEdt.setText("Yes");
resultOfferImage.setImageResource(R.drawable.tiny_minnow);
resultOfferImageInt = R.drawable.tiny_minnow;
}
else
{
resultOfferImage.setImageResource(R.drawable.no_message_required);
offerEdt.setText("Yes");
resultOfferImageInt = R.drawable.no_message_required;
}
}
Log.d("OUTPUT_CONFIDENCE", confidence+"");
progressDialog.dismiss();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
此片段由 InputFragment.java 调用,如您所见,所有字段都由方法processFinish()
填充,这是一个回调函数。由于在InputFragment.java
中完成网络操作,将调用此函数。但问题是,当InputFragment.java
调用此片段时,它会显示所有字段和图像。现在,如果我旋转设备,所有字段都会显示resultOfferImage
。那里没有图像!它消失了!当显示所有其他动态分配的值时,为什么这个图像会丢失?
请注意,我必须将代码if(savedInstanceState==null)
放在onCreateView()
中,否则会开始在轮换时显示ProgressDialog
并且不执行任何操作。这有什么不对?为什么这个图像在旋转时会丢失?
答案 0 :(得分:0)
尝试设置
setRetainInstance(true);
InputFragment.java
的onCreateView()方法中的