DialogFragment on Fragment with screen rotation - android

On my main activity I have a Fragment in which I apply setRetainInstance(true) so that the AsyncTask I use into it is not disturbed by orientation change.
A lot of work is processed by the AsyncTask. That's why I would like to display a dialog with a progressBar on top of my activity.
I made some researches and I succeed in doing with a DialogFragment:
public class DialogWait extends DialogFragment {
private ProgressBar progressBar;
public DialogWait() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_wait, container);
Dialog dialog = getDialog();
dialog.setTitle("Hello");
setCancelable(false);
progressBar = (ProgressBar) view.findViewById(R.id.progress);
return view;
}
public void updateProgress(int value) {
progressBar.setProgress(value);
}
And here is my AsyncTask:
public class InitAsyncTask extends AsyncTask<Void, Integer, Void> {
private Context activity;
private OnTaskDoneListener mCallback;
private DialogWait dialog;
public InitAsyncTask(Context context, OnTaskDoneListener callback, DialogWait dialogWait) {
activity = context;
mCallback = callback;
dialog = dialogWait;
}
#Override
protected Void doInBackground(Void... params) {
doStuff();
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
dialog.updateProgress(values[0]);
}
#Override
protected void onPostExecute(Void result) {
publishProgress(100);
if(dialog != null)
dialog.dismiss();
mCallback.onTaskDone();
}
private void doStuff() {
//...
}
}
If I don't change the screen rotation, it works fine. But if I do, the dialog is dismissed and a few seconds later, I got a NullPointerEsception which nonsense since I set the condition: if(dialog != null)
What am I doing wrong?

Solution found!
I was not doing the right thing with the Fragment containing my AsyncTask.
Because, I haven't really understood the concept of orientation in Fragment, I get it thanks to this link: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Override onCreate, and onDestroyView methods in your DialogWait as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance()) {
getDialog().setDismissMessage(null);
}
super.onDestroyView();
}

Related

Toast in OnPostExecute() once the fragment changes

I have two fragments in my Activity : Fragment_A and Fragment_B.
In Fragment A, I created an AsyncTask (when the user "swipeRefreshes" the screen). In the onPostExecute() of this task, I want to display a Toast :
private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
private Exception mLastError = null;
MakeRequestTask() {
//Some stuff
}
#Override
protected List<String> doInBackground(Void... params) {
//Some stuff
}
#Override
protected void onPreExecute() {
//Some stuff
}
#Override
protected void onPostExecute(List<String> output) {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(getActivity(), "TO_DISPLAY", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCancelled() {
swipeRefreshLayout.setRefreshing(false);
//Some stuff
}
}
If the user changes from Fragment_A to Fragment_B before the AsyncTask finishes, I get a crash:
java.lang.IllegalStateException: Fragment Fragment_A not attached to a context.
I know how to avoid the crash (by adding the condition isAdded()), but I want my Toast to be displayed no matter which Fragment is displayed/alive on top of my Activity.
1stly I would like to suggest you, please make your MakeRequestTask inner class as static as this can be a memory leak.
For your question, You need to pass the context to the class like below:
private static class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
private Exception mLastError = null;
private WeakReference<Context> weakReference;
MakeRequestTask(Context context) {
//Some stuff
weakReference = new WeakReference<>(context);
}
#Override
protected List<String> doInBackground(Void... params) {
//Some stuff
}
#Override
protected void onPreExecute() {
//Some stuff
}
#Override
protected void onPostExecute(List<String> output) {
// swipe layout will not be shown if fragment is not visible or destroyed
if (isFragmentVisible) {
swipeRefreshLayout.setRefreshing(false);
}
// toast will be shown no matter what fragment is visible
if (weakReference != null) {
Context context = weakReference.get();
if (context != null) {
Toast.makeText(context, "TO_DISPLAY", Toast.LENGTH_SHORT).show();
}
}
}
#Override
protected void onCancelled() {
if (isFragmentVisible) {
swipeRefreshLayout.setRefreshing(false);
}
//Some stuff
}
}
Try this way
Declare a boolean in Fragment_A
private boolean isFragmentVisible=false;
In Fragment_A class
Make this boolean true in onCreateView() of this Fragment_A
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.lyourlayout, container, false);
isFragmentVisible = true;
return view;
}
And make this boolean false in onDestroyView() of this fragment A
#Override
public void onDestroyView() {
super.onDestroyView();
isFragmentVisible = false;
}
Finally use it in Asyntask of Fragment_A like this
private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
private Exception mLastError = null;
MakeRequestTask() {
//Some stuff
}
#Override
protected List<String> doInBackground(Void... params) {
//Some stuff
}
#Override
protected void onPreExecute() {
//Some stuff
}
#Override
protected void onPostExecute(List<String> output) {
// swipe layout will not be shown if fragment is not visible or destroyed
if(isFragmentVisible){
swipeRefreshLayout.setRefreshing(false);
}
// toast will be shown no matter what fragment is visible
Toast.makeText(getActivity(), "TO_DISPLAY", Toast.LENGTH_SHORT).show();
}
#Override
protected void onCancelled() {
if(isFragmentVisible){
swipeRefreshLayout.setRefreshing(false);
}
//Some stuff
}
}
Or you can just use and interface or an EventBus in the onPostExecute method, and show the Toast inside the activity.
#Override
protected void onPostExecute(List<String> output) {
swipeRefreshLayout.setRefreshing(false);
activityContractInterface.showToast()
}
}
And in your Activity:
#Override
public void showToast(){
Toast.makeText(getActivity(), "TO_DISPLAY", Toast.LENGTH_SHORT).show();
}
Or the EventBus approach:
#Override
protected void onPostExecute(List<String> output) {
swipeRefreshLayout.setRefreshing(false);
EventBus.getDefault().post(new ShowToastEvent())
//just create an empty class, hope you know what EventBus is
}
And in your activity:
#Subscribe(threadMode = ThreadMode.Main){
Toast.makeText(getActivity(), "TO_DISPLAY", Toast.LENGTH_SHORT).show();
}
EventBusLibrary

NullPointerException - Handler.removeCallbacks(java.lang.Runnable)

I got some crash reports from my app on console. I tried to fix it but I stucked. I'm using custom progress dialog, my problem is about that. What should I do? Should I use removeCallBack?
FlipProgressDialog fpd; // this is my custom progress dialog
#Override
public void onPause() {
super.onPause();
fpd.dismissAllowingStateLoss();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_top_feeds, container, false);
this.mView = view;
fpd = new FlipProgressDialog(); //and initialized here for escape the nullpointer exception but not worked
fpd.setImageList(imageList);
fpd.setCanceledOnTouchOutside(false);
// Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
#Override
public void run() {
new Listeleme().execute();
}
};
myRunnable.run();
//handler.removeCallbacks(myRunnable);
//I guess I need call the above but I couldn't apply properly
return view;
}
private class Listeleme extends AsyncTask<Void,Void,Void>
{
String URL="...";
#Override
protected void onPreExecute()
{
super.onPreExecute();
fpd.show(getActivity().getFragmentManager(),"");
}
#Override
protected Void doInBackground(Void... params) {
//...
return null;
}
#Override
protected void onPostExecute(Void avoid)
{
if (getActivity()!= null) {
}
if (fpd.isVisible()) {
fpd.dismissAllowingStateLoss();
}
}
}
And this is the line 245 - FlipProgressDialog. java
#Override
public void onDestroyView() {
Dialog dialog = getDialog();
if (dialog != null && getRetainInstance()) {
dialog.setDismissMessage(null);
}
handler.removeCallbacks(r); // line 245
super.onDestroyView();
}
Apparently, some resources of your FlipProgressDialog are destroyed when you enter onPause(), so calling anything after everything gets destroyed causes that exception. Just try to call fpd.dismissAllowingStateLoss() before super.onPause()

Android Fragment with attached AsyncTask gives null after commit in Activity

I try to understand basic design pattern behind implementing AsyncTask and attaching it to Fragment. I follow numerous tutorials and implemented code (which gives nothing):
1) my Fragment class which contains AsyncTask:
public class MyFragment extends Fragment
{
public interface TaskCallback
{
void onPreExecute();
void onProgressUpdate(Integer... i);
void onPostExecute();
}
private TaskCallback mCallback;
private Task mTask;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
mCallback = (TaskCallback) activity;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
mTask = new Task();
mTask.execute();
}
private class Task extends AsyncTask<Void, Integer, Void>
{
#Override
protected void onPreExecute()
{
mCallback.onPreExecute();
}
#Override
protected Void doInBackground(Void... params)
{
for (int i=0; i<100; i++)
{
publishProgress(i*10);
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values)
{
mCallback.onProgressUpdate(values);
}
#Override
protected void onPostExecute(Void aVoid)
{
mCallback.onPostExecute();
}
}
}
2) my Main Activity code
public class MainActivity extends ActionBarActivity implements MyFragment.TaskCallback
{
private ProgressBar mProgress;
private MyFragment mTaskFragment;
private final static String TAG_FRAGMENT = "Fragment Task";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
mProgress.setProgress(0);
//create fragment manager and fragment instance
FragmentManager mFM = getSupportFragmentManager();
mTaskFragment = (MyFragment) mFM.findFragmentByTag(TAG_FRAGMENT);
if(mFM == null)
{
mTaskFragment = new MyFragment();
mFM.beginTransaction().add(mTaskFragment, TAG_FRAGMENT).commit();
}
//I added this method, but to no help, the same without this method
mFM.executePendingTransactions();
}
#Override
public void onPreExecute()
{
mProgress.setVisibility(View.VISIBLE);
}
#Override
public void onProgressUpdate(Integer... i)
{
mProgress.setProgress(i[0]);
}
#Override
public void onPostExecute()
{
mProgress.setVisibility(View.INVISIBLE);
}
}
Basically, when run the code by Debug, after
mTaskFragment = (MyFragment) mFM.findFragmentByTag(TAG_FRAGMENT);
if(mFM == null)
{
mTaskFragment = new MyFragment();
mFM.beginTransaction().add(mTaskFragment, TAG_FRAGMENT).commit();
}
Gives mTaskFragment = null
I suppose here is the problem (mTaskFragment = null), the code doesn't create instance of MyFragment.
The question: how should I change the code to update ProgressBar from background by using this (Fragment + AsyncTask) pattern?
another question: Fragment onAttached(Activity a) is deprecated, now we should use onAttached(Context context), does it mean it should be implemented like: mCallback = (TaskCallback) context;?
Replace if(mFM == null) with if(mTaskFragment == null) you want to check if the fragment is null not the FragmentManager.
Also the updated method for:
public void onAttach(Activity activity) is public void onAttach(Context context)
Using: mCallback = (TaskCallback) context; is fine as Context is a superclass of Activty (just make sure your Activity is implementing the interface)

OrientationChange handling Activity, Fragment, AsyncTask and DialogFragments?

Hi there I'm thinking about what is the correct and best way to handle Activity, Fragment, AsyncTask and DialogFragments together.
My current state is that I start my Activity and replace its ContentView with my Fragment, in which I got an EditText and one Button.
Tapping my Button executes an AsyncTasks which Requests random things and takes some time. Meanwhile I display a DialogFragment begging for patience.
Desired behavior is that, e.g. I rotate my screen my DialogFragment keeps being displayed for the time my AsyncTask is running. After that I want to show up a simple toast displaying the information I got from my HttpRequest.
Compact overview about how I thought it would work:
BaseFragment keeps a WeakReference to the Activity it's attached to
AsyncTask keeps a WeakReference to Fragment which exectures it
AsyncTasks onPreExecute() shows up the DialogFragment
AsyncTasks onPostExecute() dissmisses the DialogFragment
BaseFragment holds DialogFragment
Unfortunately this is not the way it works, on orientation change my DialogFragment keeps being displayed and no toast is showing up.
What am I doing wrong ?
public class BaseFragment extends Fragment{
private static final String TAG = BaseFragment.class.getSimpleName();
protected WeakReference<AppCompatActivity> mActivity;
private TemplateDialogFragment dialogFragment;
public WeakReference<AppCompatActivity> getAppCompatActivity(){ return mActivity; }
#Override
public void onAttach(Context context) {
if(!(context instanceof AppCompatActivity)) {
throw new IllegalStateException(TAG + " is not attached to an AppCompatActivity.");
}
mActivity = new WeakReference<>((AppCompatActivity) context);
super.onAttach(context);
}
#Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
#Override
public void onStart() {
super.onStart();
showContent();
}
public void showContent(){
}
public void showDialog(String title, String content){
dialogFragment = new TemplateDialogFragment();
Bundle bundle = new Bundle();
bundle.putString(TemplateDialogFragment.DIALOG_TITLE, title);
bundle.putString(TemplateDialogFragment.DIALOG_MESSAGE, content);
dialogFragment.setArguments(bundle);
dialogFragment.show(getFragmentManager(), TemplateDialogFragment.FRAGMENT_TAG);
}
public void notifyTaskFinished(String result) {
dismissDialog();
if(mActivity != null && !mActivity.get().isFinishing()) {
Toast.makeText(mActivity.get().getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
private void dismissDialog(){
if(dialogFragment != null && dialogFragment.isAdded()) {
dialogFragment.dismissAllowingStateLoss();
}
}
}
...
public class TemplateFragment extends BaseFragment {
private static final String TAG = TemplateFragment.class.getSimpleName();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_fragment, container, false);
}
#Override
public void showContent() {
super.showContent();
Button startTask = (Button) getAppCompatActivity().get().findViewById(R.id.button0);
final BaseFragment instance = this;
startTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomAsyncTask task = new CustomAsyncTask(instance);
EditText input = (EditText) getAppCompatActivity().get().findViewById(R.id.text0);
task.execute(input.getText().toString());
}
});
}
private static class CustomAsyncTask extends AsyncTask<String, Void, String> {
WeakReference<BaseFragment> weakBaseFragmentReference;
private CustomAsyncTask(BaseFragment fragment) {
weakBaseFragmentReference = new WeakReference<>(fragment);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
weakBaseFragmentReference.get().showDialog("Executing", "Working on the request...");
}
#Override
protected String doInBackground(String... params) {
HttpURLConnection con = HttpUrlConnectionFactory.createUrlConnection("https://www.httpbin.org/bytes/" + (params[0] == null ? "1" : params[0]));
return HttpRequester.doGet(con).getResponseAsString();
}
#Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
if(weakBaseFragmentReference.get() == null) {
return;
}
weakBaseFragmentReference.get().notifyTaskFinished(response);
}
}
}
*Edit:
After some time researching this theme I'm sure a Service is the best solution for most of my field of use. Also I used AsyncTaskLoaders a lot, because there is a smooth control of lifecycle....
Use progress bar instead of DialogFragment.
AsyncTask should only be used for tasks that take quite few seconds.
AsyncTask doesn't respect Activity lifecycle, and can lead to memory leaks.
Check some gotchas.
You can try AsyncTaskLoader to survive configuration changes.

correct implementation of MaterialDialog in DialogFragment

I'm implementing ProgressDialog that will be rotation proof, can be dismissed outside the class and should take content text as a parameter however after analysing samples and documentation I still have problem with rotations. What's the best pattern? I've tried to implement it as a singleton as well to pass content text.
public class ProgressDialog extends DialogFragment {
public static void show(FragmentActivity context) {
ProgressDialog dialog = new ProgressDialog();
dialog.show(context.getSupportFragmentManager(), "progress");
}
public static void dismiss(FragmentActivity context) {
DialogFragment progressDialog = (DialogFragment) context.getSupportFragmentManager().findFragmentByTag("progress");
if(context != null && progressDialog!=null) {
progressDialog.dismiss();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onDestroyView() {
super.onDestroyView();
if(this.getDialog() != null && getRetainInstance()) {
getDialog().setDismissMessage(null);
}
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new MaterialDialog.Builder(getActivity())
.title("title")
.content("content")
.progress(true, 0)
.progressIndeterminateStyle(false)
.cancelable(false)
.show();
}
}

Categories

Resources