user3369427
user3369427

Reputation: 435

AsnycTask Not Working

EDIT
Okay really simplified version, let's call this my Activity2

public class test {
    String username, id;
    TextView view1, view2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view1 = (TextView)findViewById(R.id.textView1);
        view2 = (TextView)findViewById(R.id.textView2);

        new testFunction().execute();

        view1.setText(username);
        view2.setText(id);
     }

     private class testFunction extends AsyncTask<Void, Void, Void> {

         @Override
         protected Void doInBackground(Void... x) {
             username = "1";
             id = "2";
             return null;
         }
      }
}

Activity1 has a submit button that starts Activity2 (this activity). After Activity2 calls onCreate(), "12" will appear on the screen. When I click back (destroy Activity2 and go back to Activity1), AND THEN go back to Activity2 again, nothing appears on the screen. Why is this?

Upvotes: 0

Views: 75

Answers (4)

Dehan Wjiesekara
Dehan Wjiesekara

Reputation: 3182

I have add some code lines to your onCreate method

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_user_data);

     //get the values from previous avtivity
     Bundle bundle=getIntent().getExtras();
     username=bundle.getString("username").toString();
     id=bundle.getString("id").toString();

     String result = new testFunction().execute("1", "2").get();
     TextView view1 = (TextView)findViewById(R.id.textView1);
     TextView view2 = (TextView)findViewById(R.id.textView2);
     view1.setText(username);
     view2.setText(id);
}

in order to get the values you must pass the values from previous activity, I guess you have did it correctly, what you missed is, you forgot to get the Intent values and assign them to username,id variables

Upvotes: 0

Stenes
Stenes

Reputation: 53

You have to store the username and id value when closing the fragment. Have a look into Fragment lifecycle onSaveInstanceState.

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(ARG_NAME, username);
outState.putBoolean(ARG_ID, id);

Upvotes: 0

Shivansh Saxena
Shivansh Saxena

Reputation: 196

finish the actvity when you have pressed back button like Intent i = new Intent(this,Here is your class name.Class);

startActivity(i);
finish();

by this whenever you will go in second act by press submit button that time on create call and you can also call your asnyc task in on resume method every time on resume will call.

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

This is one problem

new testFunction().execute("1", "2").get();

You should not use get(). It blocks the ui thread waiting for the result. And you should never block the ui thread. Use

new testFunction().execute("1", "2");

Declare the variables as instance variables

TextView view1,view2;
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_user_data);
     view1 = (TextView)findViewById(R.id.textView1); 
     view2 = (TextView)findViewById(R.id.textView2);
     new testFunction().execute("1", "2")

Then in Asynctask onPostExecute update the views

@override
protected void onPostExecute(String result)
{
   super.onPostExecute(result);
   // update view here
}

Upvotes: 1

Related Questions