打开活动时重新运行Android AsyncTask(Android AsyncTask re

系统教程 行业动态 更新时间:2024-06-14 16:57:39
打开活动时重新运行Android AsyncTask(Android AsyncTask re-runs when activity is opened)

在我的应用程序中,用户必须登录。

他们在登录页面上打开应用程序 他们输入电子邮件/密码并点击登录 打开一个具有旋转圆圈并运行AsyncTask的LoadingScreenActivity,该AsyncTask将转到我的数据库并检索所有用户信息

AsyncTask完成后,它启动了一个启动MainPageActivity的意图。

目前有两个问题:

如果用户登录然后在应用程序加载时进入主屏幕,则MainPageActivity将在准备就绪后立即打开(在现有主页的顶部),即使应用程序已最小化 如果用户登录然后在应用程序加载时转到主屏幕,然后返回到加载屏幕,AsyncTask将完成两次

问题1.目前,我在LoadingScreenActivity中的onPostExecute()方法如下所示:

@Override public void onPostExecute() { //open the main page Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class); mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK ); startActivity(mainPage); }

如果主页面活动应该打开,有没有办法在这种方法中检测到?

对于问题2.我已经完成了一个完整的障碍,有没有办法检测活动是否只是重新打开而不是第一次启动? 我真的很感激有关这方面的任何提示,我对android很新,所以我甚至不相信Async任务就是这样的。

谢谢你的时间

LoadingScreenActivity.java

public class LoadingScreenActivity extends Activity implements TaskFragment.TaskCallbacks { private static final String TAG_TASK_FRAGMENT = "task_fragment"; private TaskFragment mTaskFragment; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); FragmentManager fm = getFragmentManager(); mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT); // If the Fragment is non-null, then it is currently being // retained across a configuration change. if (mTaskFragment == null) { mTaskFragment = new TaskFragment(); fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit(); } setContentView(R.layout.loading_screen); ActionBar actionBar = getActionBar(); actionBar.hide(); TextView title = (TextView) findViewById(R.id.loading_title); TextView progress = (TextView) findViewById(R.id.loading_progress); title.setText(R.string.app_name); progress.setText("Loading your info"); } @Override public Context onPreExecute() { return getApplicationContext(); } @Override public void onProgressUpdate(int percent) { } @Override public void onCancelled() { Intent login = new Intent(getApplicationContext(), LoginActivity.class); login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(login); finish(); } @Override public void onPostExecute() { //open the main page Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class); mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK ); startActivity(mainPage); } }

和TaskFragment.java

public class TaskFragment extends Fragment { static interface TaskCallbacks { Context onPreExecute(); void onProgressUpdate(int percent); void onCancelled(); void onPostExecute(); } private TaskCallbacks mCallbacks; private DummyTask mTask; @Override public void onAttach(Activity activity) { super.onAttach(activity); mCallbacks = (TaskCallbacks) activity; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Retain this fragment across configuration changes. setRetainInstance(true); // Create and execute the background task. mTask = new DummyTask(); mTask.execute(); } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } private class DummyTask extends AsyncTask<Void, Integer, Void> { Context context; boolean running = true; @Override protected void onPreExecute() { if (mCallbacks != null) { context = mCallbacks.onPreExecute(); } } @Override protected Void doInBackground(Void... ignore) { //Get the current thread's token synchronized (this) { if(running){ DatabaseHandler dbHandler = new DatabaseHandler(context); dbHandler.populateSQLiteDatabase(); } } return null; } @Override protected void onProgressUpdate(Integer... percent) { if (mCallbacks != null) { mCallbacks.onProgressUpdate(percent[0]); } } @Override protected void onCancelled() { if (mCallbacks != null) { mCallbacks.onCancelled(); } } @Override protected void onPostExecute(Void ignore) { if (mCallbacks != null) { mCallbacks.onPostExecute(); } } } }

In my App the user has to login.

They open the app on the login page They enter email/password and hit login A LoadingScreenActivity is opened that has a swirly circle and is running an AsyncTask that goes to my database and retrieves all the users info

After the AsyncTask is completed it starts an intent to launch MainPageActivity.

There are two problems with this at the moment:

If the user logs in and then goes to the home screen while the app loads the MainPageActivity will open as soon as it is ready (on top of the existing home page) even though the app has been minimised If the user logs in and then goes to the home screen while the app loads and then returns to the loading screen the AsyncTask will complete twice over

For problem 1. At the moment my onPostExecute() method in LoadingScreenActivity looks like this:

@Override public void onPostExecute() { //open the main page Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class); mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK ); startActivity(mainPage); }

Is there a way I could detect in this method if the main page activity should be opened yet?

For problem 2. I've hit a complete road block on this, is there a way to detect if the activity has simply been re opened rather than started for the first time? I'd really appreciate any tips on this, I'm quite new to android so I'm not even convinced an Async task is the way to go with this.

Thanks for your time

LoadingScreenActivity.java

public class LoadingScreenActivity extends Activity implements TaskFragment.TaskCallbacks { private static final String TAG_TASK_FRAGMENT = "task_fragment"; private TaskFragment mTaskFragment; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); FragmentManager fm = getFragmentManager(); mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT); // If the Fragment is non-null, then it is currently being // retained across a configuration change. if (mTaskFragment == null) { mTaskFragment = new TaskFragment(); fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit(); } setContentView(R.layout.loading_screen); ActionBar actionBar = getActionBar(); actionBar.hide(); TextView title = (TextView) findViewById(R.id.loading_title); TextView progress = (TextView) findViewById(R.id.loading_progress); title.setText(R.string.app_name); progress.setText("Loading your info"); } @Override public Context onPreExecute() { return getApplicationContext(); } @Override public void onProgressUpdate(int percent) { } @Override public void onCancelled() { Intent login = new Intent(getApplicationContext(), LoginActivity.class); login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(login); finish(); } @Override public void onPostExecute() { //open the main page Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class); mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK ); startActivity(mainPage); } }

and TaskFragment.java

public class TaskFragment extends Fragment { static interface TaskCallbacks { Context onPreExecute(); void onProgressUpdate(int percent); void onCancelled(); void onPostExecute(); } private TaskCallbacks mCallbacks; private DummyTask mTask; @Override public void onAttach(Activity activity) { super.onAttach(activity); mCallbacks = (TaskCallbacks) activity; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Retain this fragment across configuration changes. setRetainInstance(true); // Create and execute the background task. mTask = new DummyTask(); mTask.execute(); } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } private class DummyTask extends AsyncTask<Void, Integer, Void> { Context context; boolean running = true; @Override protected void onPreExecute() { if (mCallbacks != null) { context = mCallbacks.onPreExecute(); } } @Override protected Void doInBackground(Void... ignore) { //Get the current thread's token synchronized (this) { if(running){ DatabaseHandler dbHandler = new DatabaseHandler(context); dbHandler.populateSQLiteDatabase(); } } return null; } @Override protected void onProgressUpdate(Integer... percent) { if (mCallbacks != null) { mCallbacks.onProgressUpdate(percent[0]); } } @Override protected void onCancelled() { if (mCallbacks != null) { mCallbacks.onCancelled(); } } @Override protected void onPostExecute(Void ignore) { if (mCallbacks != null) { mCallbacks.onPostExecute(); } } } }

最满意答案

在你的活动清单中只需添加android:configChanges="keyboardHidden|orientation|screenSize"并在活动中实现这个

@Override public void onConfigurationChanged(Configuration newConfig) { };

in your activity in the manifest just add android:configChanges="keyboardHidden|orientation|screenSize" and in the activity implement this

@Override public void onConfigurationChanged(Configuration newConfig) { };

更多推荐

本文发布于:2023-04-13 12:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/00a0f844ebd1d1e5d4cb6ba900cb1f4f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   AsyncTask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!