我目前有两个活动在做HTTP请求。在第二个活动上,有一个前一个按钮。我希望能够在不丢失信息的情况下重新开始第一项活动,因为目前我丢失了所有已恢复的信息。
第一项活动:
public class GetChildrenList extends AppCompatActivity implements View.OnClickListener {
private ArrayList<Child> childrenImeList = new ArrayList<Child>();
private ListView itemsListView;
private TextView tv_signin_success;
String email;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_children_list);
String infos_user = (String) getIntent().getSerializableExtra("infos_user");
tv_signin_success = (TextView) findViewById(R.id.tv_signin_success);
tv_signin_success.setText("Bonjour " + infos_user + "!");
itemsListView = (ListView)findViewById(R.id.list_view_children);
new GetChildrenAsync().execute();
}
class GetChildrenAsync extends AsyncTask<String, Void, ArrayList<Child>> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(GetChildrenList.this, "Please wait", "Loading...");
}
@Override
protected ArrayList<Child> doInBackground(String... params) {
InputStream is = null;
int statusCode = 0;
int id = 0;
int age = 0;
email = (String) getIntent().getSerializableExtra("email");
password = (String) getIntent().getSerializableExtra("password");
String result = null;
String first_name = null;
String last_name = null;
try {
//Http request to communicate with our system
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
URI website = new URI("http://192.168.1.33:5000/childrenime/list");
httpGet.setURI(website);
String base64EncodedCredentials = Base64.encodeToString(
(email + ":" + password).getBytes(),
Base64.NO_WRAP);
httpGet.setHeader("Authorization", "Basic " + base64EncodedCredentials);
HttpResponse response = httpClient.execute(httpGet);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
String jsonResult = "{ \"children\":" + result + "}";
Log.d("result1", jsonResult);
//Manage JSON result
JSONObject jsonObject = new JSONObject(jsonResult);
JSONArray childrenArray = jsonObject.getJSONArray("children");
for (int i = 0; i < childrenArray.length(); ++i) {
JSONObject child = childrenArray.getJSONObject(i);
id = child.getInt("id");
first_name = child.getString("first_name");
last_name = child.getString("last_name");
age = child.getInt("age");
String name = first_name + " " + last_name;
childrenImeList.add(new Child(id,name,age));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return childrenImeList;
}
@Override
protected void onPostExecute(final ArrayList<Child> childrenListInformation) {
loadingDialog.dismiss();
CustomListChildrenAdapter adapter = new CustomListChildrenAdapter(GetChildrenList.this, childrenListInformation);
itemsListView.setAdapter(adapter);
}
}
}
第二项活动:
public class GetLearningGoalsList extends AppCompatActivity implements View.OnClickListener {
private ArrayList<LearningGoal> childrenLearningList = new ArrayList<LearningGoal>();
private Button btn_previous;
private ListView itemsListView;
String email;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_learning_goals_list);
btn_previous = (Button) findViewById(R.id.btn_previous);
btn_previous.setOnClickListener(this);
itemsListView = (ListView)findViewById(R.id.list_view_learning_goals);
new GetLearningGoalsAsync().execute();
}
@Override
public void onClick(View v) {
Intent myIntent = new Intent(GetLearningGoalsList.this, GetChildrenList.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
finish();
return;
}
class GetLearningGoalsAsync extends AsyncTask<String, Void, ArrayList<LearningGoal>> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(GetLearningGoalsList.this, "Please wait", "Loading...");
}
@Override
protected ArrayList<LearningGoal> doInBackground(String... params) {
InputStream is = null;
int statusCode = 0;
int id = 0;
email = (String) getIntent().getSerializableExtra("email");
password = (String) getIntent().getSerializableExtra("password");
int idChild = (int) getIntent().getSerializableExtra("idChild");
String json = null;
String result = null;
String name = null;
String start_date = null;
String end_date = null;
try {
//Http request to communicate with our system
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
URI website = new URI("http://192.168.1.33:5000/learningchild/list");
httpPost.setURI(website);
JSONObject jsonObj = new JSONObject();
jsonObj.accumulate("idChild", idChild);
json = jsonObj.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
String base64EncodedCredentials = Base64.encodeToString(
(email + ":" + password).getBytes(),
Base64.NO_WRAP);
httpPost.setHeader("Authorization", "Basic " + base64EncodedCredentials);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
String jsonResult = "{ \"learningGoals\":" + result + "}";
Log.d("result1", jsonResult);
//Manage JSON result
JSONObject jsonObject = new JSONObject(jsonResult);
JSONArray learningGoalsArray = jsonObject.getJSONArray("learningGoals");
for (int i = 0; i < learningGoalsArray.length(); ++i) {
JSONObject learningGoal = learningGoalsArray.getJSONObject(i);
id = learningGoal.getInt("id");
name = learningGoal.getString("name");
start_date = learningGoal.getString("start_date");
end_date = learningGoal.getString("end_date");
childrenLearningList.add(new LearningGoal(id,name,start_date,end_date));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return childrenLearningList;
}
@Override
protected void onPostExecute(final ArrayList<LearningGoal> learningListInformation) {
loadingDialog.dismiss();
CustomListLearningGoalAdapter adapter = new CustomListLearningGoalAdapter(GetLearningGoalsList.this, learningListInformation);
itemsListView.setAdapter(adapter);
}
}
}
我怎能这样做,以免丢失信息?
谢谢。
答案 0 :(得分:0)
您可以使用SharedPreferences
保存数据。
首先,在第二个活动上创建SharedPreferences:
SharedPreferences pref = getApplicationContext()
.getSharedPreferences("MyPreferences", MODE_PRIVATE);
Editor editor = pref.edit();
然后将您的数据存储为KEY/VALUE
对:
editor.putString("key", "string value");
editor.commit();
然后获取第一项活动的数据:
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
String value = mSharedPreference.getString("key", "");
在Android Example上,您可以遵循一些SharedPreferences基础知识。
答案 1 :(得分:0)
嗯,首先要知道的是,有几种方法可以保留信息,这取决于具体内容和方式。
可以通过覆盖“onSaveInstanceState”方法来保存数据以保存数据:
@Override
protected void onSaveInstanceState(Bundle iOutState)
{
super.onSaveInstanceState(iOutState);
iOutState.putString("USER_INFORMATION", infos_user);
iOutState.putInt("CHILD_ID", idChild);
}
并覆盖“onRestoreInstanceState”方法以加载保存的数据:
@Override
protected void onRestoreInstanceState(Bundle iSavedInstanceState)
{
super.onRestoreInstanceState(iSavedInstanceState);
infos_user = iSavedInstanceState.getString("USER_INFORMATION");
idChild = iSavedInstanceState.getInt("CHILD_ID");
}
因此,当活动状态发生变化时,它会保存变量的最后值,并在必要时恢复它们。
此外,您没有提供足够的信息,但如果我理解正确,您首先调用“GetChildrenList”活动,然后“GetLearningGoalsList”,最后您单击上一个按钮返回“GetChildrenList”。 IF 就是这种情况,您需要了解默认情况下,活动不像单个实体那样作为单个实体运行,而是在每个“startActivity”调用中创建。换句话说,每次调用“startActivity”命令时,都会创建一个全新的活动,因此最终会有3个活动;类“GetChildrenList”中的2个和“GetLearningGoalsList”中的一个。要解决这个问题:
祝你好运=)