-1

I'm having an issue with an acitivity for an android app, to make it simple I let there be three activities : the main M, the one that answers A and the one I start C.

So M starts A using startActivityOnResult, then A sends result to M and from there M starts S.

But for some reason, the layout of S doesn't update until the main method I call from there is finished (and when that method is finished the activity goes back to M anyway, so basically I only see the layout if I make the activity S not give the result and stand there doing nothing).

Here is some of the code :

In M starting S :

Intent intent = new Intent(MainActivity.this, getCamFind.class);

    intent.putExtra("path", mCurrentPhotoPath);
    startActivityForResult(intent, CAMFIND_REQUEST_CODE);

In S :

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_getcamfind);
    receivedIntent = getIntent();
    Log.d(TAG, "Received string : " + receivedIntent.getStringExtra("path"));

}

public void onStart(){
    super.onStart();
    try {
        postCamResult(receivedIntent.getStringExtra("path"));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

So here the main method I was talking about is postCamResult, it basically connects to a server using retrofit. At the end of this method is where I would usually put the few lines to send back the result intent. The problem being the layout waits for this method to finish for some reason ? so if I don't send the result intent, it will, after doing all the server related stuff, finally print my layout, but you know, I'd like my layout to appear as soon as I open the activity!

Thank you in advance !

Dominus
  • 808
  • 11
  • 25

1 Answers1

1

The Android documentation for the Activity lifecycle states that the visible lifetime of an Activity is between onStart() and onStop(), so the layout you set will be rendered after the onStart method is done. If you put your postCamResult() inside onResume() you should be fine.

Still connecting to a server on the UI Thread is a bad idea, you should start a seperate thread for that, otherwise you might get an anr.

  • Yeah I know about the thread thing, I'm just testing around a bit. Your solution didn't work unfortunately :/ – Dominus Jul 15 '15 at 14:50
  • Moving the call from `onStart()` to `onResume()` isn't going to help. You need to get the network I/O off the main thread. This is stopping the UI from updating. If you put the contents of `postCamResult()` into a background thread you should see your layout right away. – David Wasser Jul 15 '15 at 15:03
  • My bad, eventhough the activity is visible to the user between `onStart()` and `onStop()`, the renderer might not be done rendering the layout so you have to use a background Thread as David suggested. – Cookieseller Jul 15 '15 at 15:15
  • So I went on and made a background thread, and now something really peculiar happens, the layout goes back to the main activity while the thread does the server request, so the S activity layout never appears at all ? I tried calling the thread from both onResume and onCreate to see if it made any difference but nothing. – Dominus Jul 16 '15 at 14:14