0

i saw a lot of posts saying that you should put your async task inside a Fragment(i dont remember other ways), so you can rotate screen and keep it...

but its not working for me

Activity shows the Fragment;

MyGetJsonFragment frag= ....
  frag.setRetainInstance(true);
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();

        ft.replace(R.id.fragment_main, frag);
        ft.commit();

than, inside this Fragment i have a RecyclerView, and when the user click in any item on list, it call the Adapter and inside the adapter onBindViewHolder i implemented the onClick to call the AsyncTask

 holder.button_get.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new AsyncGet(activity, AdapterList.this).execute();
                    }
                });

if i use the configChanges ( android:configChanges="orientation|keyboardHidden|screenSize) at manifest it works normal, but i heard and read that this is not a good workaround!

after removed the configChanges, if i rotate the screen, the progress dialog dismiss and than on the post execute i get the error at progressdialog.dissmiss or anyother thing that dependes the activity

i understood the activity life cycle that when i rotate the original that started the async is destroyed and new one is created...

but how can i handle it and still show the progressdialog?

i need the Context/Activity at the async to start the progress and i have some db methods inside the postExecute that use Activity/Context

and why setRetainInstance(true) is not holding the Instance?

user2582318
  • 1,607
  • 5
  • 29
  • 47
  • 2
    Fragments are recreated when you rotate the screen - therefore have the same problem of restarting an AsyncTask as an Activity does, so I don't know where you read that first statement. – OneCricketeer Aug 04 '16 at 17:47
  • some posts/comments here, but nothing really official, is there anyway to still show the progress while rotating the screen? – user2582318 Aug 04 '16 at 17:50
  • 1
    Like this post? http://stackoverflow.com/a/16426730/2308683 – OneCricketeer Aug 04 '16 at 17:52
  • 1
    Possible duplicate of [Android Fragments. Retaining an AsyncTask during screen rotation or configuration change](http://stackoverflow.com/questions/8417885/android-fragments-retaining-an-asynctask-during-screen-rotation-or-configuratio) – OneCricketeer Aug 04 '16 at 17:57
  • 2
    I think the problem here is that although the Fragment is maintained, the Adapter uses the activity, which is being recreated, therefore causing the issue. – OneCricketeer Aug 04 '16 at 17:58
  • @cricket_007 but using this logic, its impossible to keep the progress dialog, since its use the first activity, than its destroyed and recreated.. i cant understand this, the only possible way, is the first activity keep hidden/live while progress dialog/async still use it, correct? – user2582318 Aug 04 '16 at 18:55
  • You can get the context from the Fragment. I don't know where you got that `activity` variable in the adapter from without seeing a [mcve] – OneCricketeer Aug 04 '16 at 18:57
  • @cricket_007 sorry the lack of MCVE, i will try to filter and post one, anyway, at the Fragment i use new Adapter(getActivity()...) – user2582318 Aug 04 '16 at 18:58
  • You could use Volley instead of an AsyncTask, couldn't you? – OneCricketeer Aug 04 '16 at 19:01
  • @cricket_007 yes i guess, i will study it, thank youuu – user2582318 Aug 04 '16 at 19:04

0 Answers0