2

I initialize a view in onCreate and sometimes after minimizing app the view is null for some users. I cannot reproduce this error. GuiReceiver is a receiver that updates Activity views.

Here is my simplified code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player_activity);
    tvTitle = (TextView) findViewById(R.id.title);
}

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

    if (tvTitle != null)
        FirebaseCrash.log("onResume - tvTitle != null");
    else
        FirebaseCrash.log("onResume - tvTitle == null");

    IntentFilter filter = new IntentFilter();
    filter.addAction(CommonConstatns.GUI_UPDATE_ACTION);

    if (receiver == null) {
        receiver = new GuiReceiver();
        receiver.setPlayerActivity(this);
    }
}    

@Override
protected void onPause() {
    super.onPause();
    if (receiver != null) {
        unregisterReceiver(receiver);
        FirebaseCrash.log("GUIReceiver unregistered");
    }
}
<activity
    android:name=".activity.PlayerActivity"
    android:launchMode="singleTask"
    android:screenOrientation="userPortrait" />
petrumo
  • 1,116
  • 9
  • 18
Mateusz Kaflowski
  • 2,221
  • 1
  • 29
  • 35
  • 1
    Android can kill your app if there is not enough memory. Then when you restore your app from the recent apps it restores activity stack but `onCreate` method doesn't get called – Volodymyr Jan 19 '17 at 15:51
  • So how do you fix it @VolodymyrKhodonovych – J Blaz Apr 25 '19 at 15:02

3 Answers3

2

Unless tvTitle is declared as an instance variable of the class, I'd move the following line of code into the onResume method:

tvTitle = (TextView) findViewById(R.id.title);
Joseph Lam
  • 5,289
  • 3
  • 14
  • 13
1

The process of your app can be killed at any time by the system, with no guarantee of onDestroy beeing called. This is probably the reason why the problem occurs for some users. You might be able to reproduce it by opening enough applications for the memory to be claimed and the app process to be killed by the system. You might try to save your instance in onPause and restore it in onResume. It is just a function call to put in both methods.

1

I have never encountered such issue, but taking into consideration suggestions written by @Volodymyr Khodonovych and @jdesesquelles, you can improve the code a little. You can check if ContentView is set in onResume() method. You can get its value in the way described here: https://stackoverflow.com/a/8817003/1150795. Next, you can check if it's different than null. If it's null, then it means it's not set, Android killed your app and you can try to set it and all views for this Activity in the same way like in onCreate(), but this time in onResume() method. I don't know if it's the best and valid solution. Maybe it occurs only on specific devices with limited resources, but I'd try to go this way in the beginning. At least you can have some sort of starting point for the solution of improving your app.

Community
  • 1
  • 1
Piotr Wittchen
  • 3,853
  • 4
  • 26
  • 39