1

In one of my android App, extending Activity, I use onResume() to refresh activity in return from PreferenceActivity, such this way ...

public class ListViewWebShort extends Activity {
    ListView listView;

    @Override
    protected void onResume() {
        super.onResume();
        this.onCreate(null);
    }
    ....
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);


        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.list);
        ....

.. and here, App works as expected.

In another App, where I extend AppCompatActivity, the same onResume() method, makes my app crash or loop on start :

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    ListView listView;
    ArrayAdapter<String> adapter;
    ....
    @Override
    protected void onResume() {
        super.onResume();
        this.onCreate(null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ....

If I use requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView, I see in debug that App loops in getDelegate().setContentView(layoutResID), inside AppCompactActivity.java, but if I remove it, the app FCs in the same function. It really makes me drive crazy .. what am I missing?

Thanks in advance for any suggestion.

gabolander
  • 11
  • 2
  • 4

2 Answers2

1

Don't call any activity lifecycle method directly. These methods are intended to be called from the system (inversion of control).

Especially in the super implementation of onCreate() there might happen something, that leads to crashes if they are called in the wrong order.

Try to extract the code, which you want to call in onCreate() and in onResume() in a separate method and call this method from onCreate() and onResume().

Christopher
  • 9,682
  • 7
  • 47
  • 76
  • This makes sense, and I will try this way, thank you a lot. But I don't understand why in many apps, using my onResume() as mentioned (and as some devs here, suggest) works prefectly and in some doesn't ... – gabolander Jan 05 '17 at 11:38
  • 1
    onResume() is not the problem. But you are calling onCreate() in your onResume() method. This is not allowed, because onCreate() is a lifecycle method. I can't believe that any serious developer would suggest you to call onCreate() in onResume(). – Christopher Jan 05 '17 at 11:44
  • Yes @Christopher, I really think you are right. I found here, for instance (http://stackoverflow.com/questions/3053761/reload-activity-in-android) but in many other posts, suggestions by whom used my old (wrong) method :-) Thank you to "wake me up" LOL – gabolander Jan 05 '17 at 12:33
0

Actually in every call of onCreate, the onResume method of the application is also called.

So just move whatever code you wrote in the onCreate to onResume.

Hope it helps.

Minasie Shibeshi
  • 390
  • 3
  • 16