24

I have an activity contains too many UI controls. I want to execute a method after make the activity visible.

An example i tried:

public class Main extends Activity{

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

    private void MyMethod(){
        Toast.makeText(this, "Hi UI is fully loaded", Toast.LENGTH_SHORT).show();
    }
}

But in the above sample, the message shows before the activity is visible.

Is there a way to find out, if the activity is fully visible ?

MichaC
  • 12,433
  • 2
  • 39
  • 53
Riskhan
  • 4,082
  • 11
  • 44
  • 70

5 Answers5

30

Move your code to onResume

@Override
protected void onResume()
{
    super.onResume();
    MyMethod();
}

Check the activity lifecycle

http://developer.android.com/reference/android/app/Activity.html

protected void onResume ()

Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

Raghunandan
  • 129,147
  • 24
  • 216
  • 249
  • 1
    This will be called before onCreateOptionsMenu which is problematic if you need to do something with that. For example, the user rotates the screen in the middle of a tool-bar search. You have to reset the search to where it was at the time of rotation. This requires knowing information only available after the call to onCreateOptionsMenu. here is the order I get onResume called onAttachedToWindow called onCreateOptionsMenu called onPrepareOptionsMenu called – Brian Reinhold Feb 23 '18 at 14:23
  • @BrianReinhold handling rotation is totally a different issue. onResume is when the user can start interacting with the ui. You should read lifecycle of activity from the android docs – Raghunandan Feb 23 '18 at 15:36
  • @Raghunadan yes it is because one starts from scratch. I wish I knew what the last call systematically delivered by the system is. But if you need to do options menu work (which I do) don't do it in onResume() – Brian Reinhold Feb 23 '18 at 18:08
7

Move the code on to onAttachedToWindow()

@Override
public void onAttachedToWindow() {
   super.onAttachedToWindow();
   Toast.makeText(this, "Hi UI is fully loaded", Toast.LENGTH_SHORT).show();
}
Haresh Chhelana
  • 23,930
  • 5
  • 51
  • 66
6

Move the code on to onResume.

System calls this method every time your activity comes into the foreground, including when it's created for the first time. Read details for Pausing and Resuming an Activity

@Override
protected void onResume() {
    super.onResume();

    MyMethod();
}

Read more at Android activity life cycle - what are all these methods for?

Community
  • 1
  • 1
Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
3

there is no real callback, which is called, exactly at that time the Activity is Visible. But as you can see in the attached picture, the method onResume() is just called when Activity should be visible.

Also have a look at the Activity lifecycle and the documentation HERE

So your method should be called like this:

@Override
public void onResume() {
    super.onResume();
     MyMethod();
}

Diagram Android

atiruz
  • 2,461
  • 22
  • 34
A.S.
  • 4,494
  • 3
  • 23
  • 41
2

Call MyMethod() in onResume() of Activity

As per documentation onResume() is called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

protected void onResume() {
   super.onResume();
   MyMethod();
}
dusan
  • 8,524
  • 2
  • 31
  • 54
Mukesh Kumar Singh
  • 4,361
  • 2
  • 20
  • 30