0

I have an activity in my android app that calls an asynctask on onCreateView()

Here is my code :

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weeklyprofit);
            fromDateTxt=(TextView)findViewById(R.id.fromDate);
            toDateTxt = (TextView)findViewById(R.id.toDate);

         GetXYPoints getXY = new GetXYPoints();
         getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());

}

now my application needs to rotate, so i need to store data when rotate :

I have implemented this as below :

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putString("from", fromDateTxt.getText().toString());
    savedInstanceState.putString("to", toDateTxt.getText().toString());

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    SystemClock.sleep(500);
    fromDateTxt.setText(savedInstanceState.getString("from"));
    toDateTxt.setText(savedInstanceState.getString("to"));

     GetXYPoints getXY = new GetXYPoints();
     getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}

So i recall the asynctask again with restored data, my problem is that the activity run again when rotate and the it calls onRestoreInstanceState so how can i prevent the activity from calling the first asyncktask?

In other way what is the best solution to store data returned by an asynck task when screen is rotate?

dotvav
  • 2,808
  • 15
  • 31
Amalo
  • 772
  • 3
  • 13
  • 32
  • SImply take one boolean flag and when first time it will be false after set to true So when flag true call AsyncTask. – Haresh Chhelana Oct 06 '15 at 09:48
  • Possible duplicate of [Don't reload application when orientation changes](http://stackoverflow.com/questions/5913130/dont-reload-application-when-orientation-changes) – Haresh Chhelana Oct 06 '15 at 09:50

2 Answers2

2

i would suggest you to to make sure you actually need your activity to be reset on a screen rotation (the default behavior). Every time I've had issues with rotation I've added this attribute to my tag in the AndroidManifest.xml, and been just fine.

android:configChanges="keyboardHidden|orientation"

source How to handle an AsyncTask during Screen Rotation?

Community
  • 1
  • 1
Aayushi
  • 574
  • 3
  • 14
  • this works, but may lead to layout-problems if you specify different layouts for landscape and portrait! – dy_ Oct 06 '15 at 09:53
  • you can also read this blog http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html for different method – Aayushi Oct 06 '15 at 09:57
  • Its working but if i'm not specifying different layout for landscape and portrait – Amalo Oct 06 '15 at 10:11
  • otheerwise you can use 1)Volley -> http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ 2)LoopJ -> http://loopj.com/android-async-http/ – Aayushi Oct 06 '15 at 10:17
1

In your AndroidManifest.xml add following for prevent reloading activity when orientation change

android:configChanges="orientation|screenSize"

So, you'll have something like this:

<activity android:name="Activity"
android:configChanges="orientation|screenSize">
</activity>

Hope it works!

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • this works, but may lead to layout-problems if you specify different layouts for landscape and portrait! – dy_ Oct 06 '15 at 09:53