gpt4 book ai didi

android - 在 Android 中停止/恢复 Activity 和 onCreate

转载 作者:太空狗 更新时间:2023-10-29 16:34:35 27 4
gpt4 key购买 nike

我是一名新的 Android 开发者,我有一个问题。用户登录后,我需要从外部 URL 获取一些数据并将它们显示在我的一个 Activity 中。但我一直在阅读并发现了这一点:

When your activity comes back to the foreground from the stopped state, it receives a call to onRestart(). The system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time).

还有这个

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

Herehere .

所以,看起来我不应该在 onCreate 方法上获取我需要的数据。那么在哪?在之前的 Activity 中并将数据保存在手机内存中?我觉得这不太好。

提前致谢。


编辑

我正在按照建议使用 AsyncTask,但每次我切换手机方向时,都会调用 onCreate 方法。

我的主要 Activity :

public class MainActivity extends Activity implements AsyncResponse {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ReadJSONTask jsonTask = new ReadJSONTask();
jsonTask.delegate = this;
jsonTask.execute("http://www.myweb.com/myscript.php");
}

@Override
public void processFinish(String output) {
Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG).show();
}
}

ReadJSONTask 类:

public class ReadJSONTask extends AsyncTask<String, Void, String> {
public AsyncResponse delegate = null;
public String res;
public Boolean finish = false;

@Override
protected String doInBackground(String... url) {
String response = null;
String adres = url[0];

URL url_HTTPRequest = null;
try {
url_HTTPRequest = new URL(adres);
response = transfer(url_HTTPRequest);

} catch (MalformedURLException e) {
Log.e("URL ERROR", "MalformedURLException");
} catch (Exception e) {
Log.e("URL ERROR", "exc");
}

return response;
}

@Override
protected void onPostExecute(String result) {
Log.d("mylog", "result= " + result);
delegate.processFinish(result);
}

public String transfer(URL url) throws IOException {

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// inputStream = url.openStream();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = bin.readLine();
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line);
sb.append("\r\n");
line = bin.readLine();
}
inputStream.close();
return sb.toString();
}
}

界面就是:

public interface AsyncResponse {
void processFinish(String output);
}

正如我所说,每次我切换手机方向时,都会执行所有异步过程(并且会显示 Toast)。这正是我一开始想要避免的。

最佳答案

你不需要停止/恢复 Activity 你可以使用AsyncTask类和doInBackGround方法从外部url获取数据并显示给用户进程对话框等待

关于android - 在 Android 中停止/恢复 Activity 和 onCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893917/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com