0

I have an onCreate() method that looks something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);        
    
    if (blah) {
        blahblah;
    } else {
        method1();
    }

    MethodA();

}

An onActivityResult method that looks something like this:

@Override
protected void onActivityResult(int a, int b, Intent c) {
    super.onActivityResult(b, a, c);
    if (b == blah) {
        if (a == blah) {
            Method1();
        }
    }
}

Method1():

public void Method1() {
    blah
}

I have a methodA() which is supposed to appear first time (at the moment it doesnt appear, unless I put methodA() in onStart() - then only it appears on its second attempt at opening the app

public void MethodA() {
   sets text on screen
   
}









 
  • android 10+ doesn't guarantee calling oncreate or ondestroy on back button or home button for optimization, you should use onstart and onpause for that purpose – Alireza Jamali Dec 30 '22 at 16:47

2 Answers2

1

Based on what you have described, seems like the onCreate() method is being returned earlier than expected.

If the layout is being set, (ie setContentView() call worked) then, MethodA() should surely execute unless, as mentioned earlier, the onCreate() method is returning earlier in either of if or else block execution. Use the debugger to perform step debugging and step over the method calls such as method1() in the if-else block.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);        
    
/* Did you check this If-Else block so that the control actually */
/* goes down below? to MethodA() */
    if (blah) {
        blahblah;
    } else {
        method1();
    }

    MethodA();

}
mahee96
  • 705
  • 6
  • 15
  • My Debuggin is not working for some reasons, I cannot create Logs. Would it be worth knowing that I have a MainActivity before this activity which also has an OnCreate method (the Log.d works there but not for the onCreate Activity mentioned here – compSciStudent Jan 06 '21 at 12:59
  • Right I have used fixed my logging and it appears that the method is definitely being executed. However the method is also 100% correct because when executed in the onStart() method instead, the expected data displayed (but only on the second time the app opens as mentioned) – compSciStudent Jan 06 '21 at 13:26
0

From my knowledge in Android it should have been called. From the information you have given there is limited debugging and searching for mistakes I can do, but I'd like to know how you are checking whether your function is or is not being called.

  • From what I read, you are only trusting on visual feedback to verify whether your function is being called.

I would not want that, especially since in a specific case of yours, you tell it does appear after two launches.

Try using Logcat (integrated by default in Android Studio, not a plugin) to check whether the function is or isn't being called. Start by verifying whether onCreate is called on launch (which should be the case in my experience).

On top of your class write the static member variable (if you write "logt" and press tab, it creates itself automatically):

private static final String TAG = "ActivityName";

In the OnCreate method write (if you write "logd" and press tab, it creates itself automatically):

log.d(TAG, "I am printing from the OnCreate method");

In Logcat you will be able to filter on your debugTagName.

https://developer.android.com/studio/debug/am-logcat

If this is not the issue, verify if you are working with fragments or activity. In containers the OnCreate method might be called earlier for fragments (when they are not on the screen). This should not cause any issues, but it might when items are programatically moved or removed from another fragment.

jetspiking
  • 304
  • 1
  • 5
  • Right I have used fixed my logging and it appears that the method is definitely being executed. However the method is also 100% correct because when executed in the onStart() method instead, the expected data displayed (but only on the second time the app opens as mentioned) – compSciStudent Jan 06 '21 at 13:26
  • Few things you can check: -Text color same as background? -Display position constraints correctly? -Items overlapping? Try doing something else in the "MethodA()" method, change the color of the background to blue / yellow for example, and see what happens. – jetspiking Jan 06 '21 at 13:33
  • So the problem was actually to do with the method being an API. The API causes a delay. It is fixed now. – compSciStudent Jan 06 '21 at 15:50
  • Happy you were able to fix it. If you found my answer helpful, please consider marking it as answer or upvoting. – jetspiking Jan 06 '21 at 16:42