在本文中,我们将详细介绍完成AsyncTask后,如何处理关闭DialogFragment的各个方面,并为您提供关于兼容性lib的相关解答,同时,我们也将为您带来关于AndroidDialogFrag
在本文中,我们将详细介绍完成AsyncTask后,如何处理关闭DialogFragment的各个方面,并为您提供关于兼容性lib的相关解答,同时,我们也将为您带来关于Android DialogFragment vs Dialog、Android DialogFragment(1)、Android DialogFragment(2)、Android Fragment DialogFragment 合肥懒皮的有用知识。
本文目录一览:- 完成AsyncTask后,如何处理关闭DialogFragment(兼容性lib)(async await兼容性)
- Android DialogFragment vs Dialog
- Android DialogFragment(1)
- Android DialogFragment(2)
- Android Fragment DialogFragment 合肥懒皮
完成AsyncTask后,如何处理关闭DialogFragment(兼容性lib)(async await兼容性)
关于如何处理AsyncTask期间的配置更改的文章很多,但是我发现没有一个关于AsyncTask完成并尝试关闭DialogFragment(兼容性库)时处于后台的应用程序(onPause())的明确解决方案。
这是问题所在,如果我运行的AsyncTask应该在onPostExecute()中关闭DialogFragment,则当应用程序试图关闭DialogFragment时,它在后台时会收到IllegalStateException。
private static class SomeTask extends AsyncTask<Void, Void, Boolean> { public SomeTask(SomeActivity tActivity) { mActivity = tActivity; } private SomeActivity mActivity; /** Set the view during/after config change */ public void setView(Activity tActivity) { mActivity tActivity; } @Override protected Boolean doInBackground(Void... tParams) { try { //simulate some time consuming process TimeUnit.SECONDS.sleep(3); } catch (InterruptedException ignore) {} return true; } @Override protected void onPostExecute(Boolean tRouteFound) { mActivity.dismissSomeDialog(); }}
该活动如下所示:
import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;public class SomeActivity extends FragmentActivity { public void someMethod() { ... displaySomeDialog(); new SomeTask(this).execute(); ... } public void displaySomeDialog() { DialogFragment someDialog = new SomeDialogFragment(); someDialog.show(getFragmentManager(), "dialog"); } public void dismissSomeDialog() { SomeDialogFragment someDialog = (SomeDialogFragment) getFragmentManager().findFragmentByTag("dialog"); someDialog.dismiss(); } ....}
正常运行,除非SomeTask仍在运行时应用程序切换到后台。在那种情况下,当SomeTask尝试关闭disSomeDialog()时,我得到一个IllegalStateException。
05-25 16:36:02.237: E/AndroidRuntime(965): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
我所见过的所有帖子似乎都通过精心设计的变通办法指向了一些混乱的方向。是否没有某种Android处理方式?如果它是一个Dialog而不是DialogFragment,那么Activity的dismissDialog()将正确处理它。如果它是真实的DialogFragment而不是ACP中的一个,则dismissAllowingStateLoss()将对其进行处理。DialogFragment的ACP版本没有这样的东西吗?
答案1
小编典典要解决非法状态异常问题并实质上实现dismissAllowingStateLoss(),可以使用以下方法来完成。
getFragmentManager().beginTransaction().remove(someDialog).commitAllowingStateLoss();
这应该在没有hacky代码的情况下解决该问题。如果您具有使用dialog.show();通过处理程序与UI线程进行通信的线程,则也可以将其应用于show。这也可能导致非法状态异常
getFragmentManager().beginTransaction().add(someDialog).commitAllowingStateLoss();
鉴于发帖人的问题,@ joneswah是正确的。如果使用支持库,请更换
getFragmentManager()
与
getSupportFragmentManager()
对于未来的Google员工:@Alex
Lockwood对该解决方案提出了良好而有效的关注。该解决方案确实可以解决该错误,并且在大多数情况下都可以使用,但是从UX的角度暗示了原始问题中的方法存在问题。
该活动应假定异步任务可能未完成并且不会执行onPostExecute()。无论启动任何UI操作(即微调器,理想情况下不是对话框)来通知用户异步操作,都应具有在超时或通过跟踪状态并检入onRestore
/ onResume类型生命周期事件来自动停止的规定,以确保用户界面已正确更新。服务可能也值得调查。
Android DialogFragment vs Dialog
谷歌建议我们使用简单的 by usingDialogFragment
代替简单Dialog
的 by using Fragments
API
,但是DialogFragment
对于简单的 Yes-No 确认消息框使用隔离是很荒谬的。在这种情况下,最佳做法是什么?
Android DialogFragment(1)
本例简单的以一个按钮启动弹出一个DialogFragment。当点击Button按钮后,弹出一个Android DialogFragment。
Java代码:
package zhangphil.dialog;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment f = new MyDialogFragment();
f.show(getFragmentManager(), "my tag");
}
});
}
private class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// DialogFragment如同AlertDialog一样,这段代码将取消标题,创建一个单纯的Frame
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
// 以下是该Fragment里面的内容
View view = inflater.inflate(android.R.layout.simple_list_item_2, container);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText("zhangphil @csdn");
text2.setText("http://blog.csdn.net/zhangphil");
return view;
}
}
}
代码运行结果:

附录:
1,《Android Material Design Dialog》链接地址:http://blog.csdn.net/zhangphil/article/details/48895503
Android DialogFragment(2)
Android DialogFragment(2)
附录文章 1 简单介绍了如何实现一个 DialogFragment,本文再介绍一种简单的方法:直接重写 DialogFragment 的 onCreateDialog 返回一个 AlertDialog 实现对话框。本文的例子和附录文章 1 不同的地方:不在依赖 onCreateView。
代码运行逻辑简述:功能简单,当点击 FloatingActionButton
后弹出一个自定义的 DialogFragment,该 DialogFragment 重写了 onCreateDialog,返回一个自定义的 AlertDialog。
package zhangphil.app;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getFragmentManager(), "zhangphil");
}
});
}
public static class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("zhang phil @ csdn");
builder.setView(view)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
}).setNegativeButton("取消", null);
return builder.create();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
代码运行结果如图:
附录文章:
1,《Android DialogFragment(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/50886077
Android Fragment DialogFragment 合肥懒皮
https://www.jianshu.com/p/971de3036bd1
我们今天的关于完成AsyncTask后,如何处理关闭DialogFragment和兼容性lib的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android DialogFragment vs Dialog、Android DialogFragment(1)、Android DialogFragment(2)、Android Fragment DialogFragment 合肥懒皮的相关信息,请在本站查询。
本文标签: