0

I have developed a application connect with wi-fi. In that app portrait/landscape transition restart the activity when rotate the phone and interrupt the socket connection. Then I add portrait to AndroidManifest.xml file then problem have been solved. I want to know that is portrait/landscape transition effect to Async-Task also?

<activity
android:name="login"
android:label="@string/login_title"
android:configChanges="orientation|screenSize" >
</activity>

Login.java file

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class login   extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            try{


             Button buttonSignin = (Button) this.findViewById(R.id.btnSignIn);
//This is the place gives nullpointerException
             buttonSignin.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        EditText user=(EditText) findViewById(R.id.txtUserName);
                        EditText pass=(EditText) findViewById(R.id.txtPassword);

                        if(user.getText().toString()== "")
                        {

                            return;
                        }

                        else if(pass.getText().toString()== "")
                        {

                            return;
                        }
                        else
                        {
                            LoginRequest reqs_login = new LoginRequest(login.this,login.this);
                            reqs_login.where="Login_Data";
                            reqs_login.title="Login";
                            reqs_login.username=user.getText().toString();
                            reqs_login.password=pass.getText().toString();

                            reqs_login.execute();
                        }

                    }
                });

            } catch (NullPointerException e) {
                e.printStackTrace();
                //Toast.makeText(getBaseContext(), "Error:1 on uplod file", Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                e.printStackTrace();
                //Toast.makeText(getBaseContext(), "Error:2 File may be already exists", Toast.LENGTH_LONG).show();

            }

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

        }


    }
Sajitha Rathnayake
  • 1,688
  • 3
  • 26
  • 47

3 Answers3

0

Use this in your AndroidManifest.xml activity,

android:configChanges="orientation|screenSize"

working for me in AsyncTask also

Karthick pop
  • 616
  • 3
  • 16
0

I guess the Answer would be NO because the Async task (Background task) once started executing inBackground it will not stop until complete execution of that code in background. No matter App is in whichever State , Even the App is minimized it does not affect the Async Task. So Changing Landscape / Portrait mode will not affect the AsyncTAsk (InBackground) method. hope this will help...

i.n.e.f
  • 1,773
  • 13
  • 22
0

What is the best way to retain active objects—such as running Threads, Sockets, and AsyncTasks—across device configuration changes?

Deprecated: Override onRetainNonConfigurationInstance()

transferring an active object across Activity instances was merely a matter of returning the active object in onRetainNonConfigurationInstance() and retrieving it in getLastNonConfigurationInstance(). As of API 13, these methods have been deprecated in favor of the more Fragment's setRetainInstance(true/false)

Recommended: Manage the Object Inside a Retained Fragment

Fragment#setRetainInstance(true) allows us to bypass this destroy-and-recreate cycle

For more details please go through the below

Handling Configuration Changes

Hope this will help you.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33