1

I'm trying to port a feature from an iOS app to an Android app. UIKit, the iOS view programming framework, provides the lifecycle method -viewDidAppear:animated:, which is called after the view controller has been presented.

The Android documentation for activity lifecycle management reads that "the foreground lifetime of an activity happens between the call to onResume() and the call to onPause()," but when I add asynchronous dispatch code to an activity's onResume(), it increases the time-to-start of that activity.

How can I start an activity and then do arbitrary work after that activity is running in the foreground?

Edit: I'm using code adapted from the Android "Controlling the Camera" training and the camera API guide; the former reads, "As Android's own Camera application does, the recommended way to access the camera is to open Camera on a separate thread that's launched from onCreate()."

I've got this at the end of my onCreate():

new Thread(new Runnable() { @Override public void run() { mCamera.setPreviewCallback(previewCb); mCamera.startPreview(); previewing = true; mCamera.autoFocus(autoFocusCB); } }).start(); 
4
  • 2
    If all you're doing in onResume() is firing off a thread or AsyncTask, it shouldn't affect time-to-start much, if at all. Maybe showing your code will reveal something? Commented Jul 29, 2013 at 17:47
  • 2
    If you're noticeably increasing the start time of your activity, you're probably not doing your work asynchronously. You might want to post that code. Commented Jul 29, 2013 at 17:47
  • 1
    It looks like onResume() is method your after. It might be an issue with setting up for the async work. Could you post that code? Commented Jul 29, 2013 at 17:52
  • I've edited my question to include my code. Commented Jul 29, 2013 at 19:30

3 Answers 3

1

In general, the Android pattern is to use AsyncTask or your own background thread to start your asynchronous work from the Activity onCreate or onResume (depending on the type of work you need to do).

However, if you really need to have the view be in a state where it can render and has measured, you can always listen to Activity onAttachedToWindow, which is called when the windows for the activity has been created and the root view has been attached to it. Note that this does not guarantee you that it has also been drawn yet, so if you run bunch of work in that method, you still can block the UI thread and make the user experience bad. So, you still need to consider starting your work on a background thread.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at AsyncTask.

This is how you would handle background tasks on the UI thread.

1 Comment

AsyncTask doesn't do work on the UI thread. It's running on its own thread. onPostExecute and onPreExecute however runs on the UI thread and should be used to present ProgressDialogs and update the UI and such (smaller operations).
0

You can try this,

Step1: Override the Tabselected method in your activity

@Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. try { if(MyEventsFragment!=null && tab.getPosition()==3) { MyEvents.fragmentChanged(); } } catch (Exception e) { } mViewPager.setCurrentItem(tab.getPosition()); } 

Step 2: Using static method do what you want in your fragment,

public static void fragmentChanged() { Toast.makeText(actvity, "Fragment Changed", Toast.LENGTH_SHORT).show(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.