Android start activity after long time on minimize
It happened because Android system destroys your activity instance. See Saving and restoring activity state section in activity lifecycle documentation:
The system may also destroy the process containing your activity to recover memory if the activity is in the Stopped state and hasn't been used in a long time, or if the foreground activity requires more resources.
To prevent this crash you have to save and restore instance state in this methods:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
}
![Misha Akopov](https://i.stack.imgur.com/1zAxp.jpg?s=256&g=1)
Misha Akopov
Ranked #1 in android for Top-Users of Georgia LinkedIn profile
Updated on June 04, 2022Comments
-
Misha Akopov about 2 years
After user goes to other application, my application is not closed (as general, it is minimized) but after some time when I open application it crashes, because NullPointerException.That is because android cleans some memory and some variables are not accessible any more. Is there any way to determine if android cleared memory of my app ? Or what is good solution to fight this issue
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_payment); Bundle bundle = getIntent().getExtras(); personId = bundle.getInt("personId"); personName = bundle.getString("personName"); lendStr = getResources().getString(R.string.lend); borrowStr = getResources().getString(R.string.borrow); setUpViews(); } private void setUpViews() { SqlHelper db = new SqlHelper(this); Helper.centerActionBarTitle(this, db.getPerson(personId).getName(), false); tf = Typeface.createFromAsset(getAssets(), "FLEXO.TTF"); tfFlexo = Typeface.createFromAsset(getAssets(), "FLEXO_BOLD.TTF"); iv_add_row = (ImageView) findViewById(R.id.iv_add_row); iv_remove_row = (ImageView) findViewById(R.id.iv_remove_row); et_amount = (EditText) findViewById(R.id.et_amount); et_item_name = (EditText) findViewById(R.id.et_item_name); et_quantity = (EditText) findViewById(R.id.et_quantity); et_price = (EditText) findViewById(R.id.et_price); et_total_price = (TextView) findViewById(R.id.et_total_price); tv_currency = (TextView) findViewById(R.id.tv_currency); et_item_name.setText(getResources().getString(R.string.item)); et_quantity.setText(getResources().getString(R.string.qty)); et_price.setText(getResources().getString(R.string.price)); et_total_price.setText(getResources().getString(R.string.total)); tv_break = (TextView) findViewById(R.id.tv_break); tv_payment_date = (TextView) findViewById(R.id.tv_payment_date); tv_p_date = (TextView) findViewById(R.id.tv_p_date); tv_p_time = (TextView) findViewById(R.id.tv_p_time2); tv_maturity = (TextView) findViewById(R.id.tv_maturity); tv_m_time = (TextView) findViewById(R.id.tv_m_time); tv_m_date = (TextView) findViewById(R.id.tv_m_date); ll_list_wrapper = (LinearLayout) findViewById(R.id.ll_list_wrapper); tv_operation_type = (TextView) findViewById(R.id.tv_operation_type); chk_break_items = (ImageView) findViewById(R.id.chk_break_items); et_item_name.setTypeface(tfFlexo); et_quantity.setTypeface(tfFlexo); et_price.setTypeface(tfFlexo); et_total_price.setTypeface(tfFlexo); tv_operation_type.setTypeface(tf); tv_currency.setTypeface(tf); tv_break.setTypeface(tf); tv_payment_date.setTypeface(tf); tv_p_date.setTypeface(tf); tv_p_time.setTypeface(tf); tv_maturity.setTypeface(tf); tv_m_time.setTypeface(tf); tv_m_date.setTypeface(tf); int currencyIndex = MainActivity.settings.getCurrency(); tv_currency.setText(Constants.currencies[currencyIndex]); }
Logcat output:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lionshare.aldkan/com.lionshare.aldkan.AddPayment}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)
at com.lionshare.aldkan.AddPayment.onCreate(AddPayment.java:114)
at android.app.Activity.performCreate(Activity.java:5066)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
... 11 moreActyually, at
com.lionshare.aldkan.AddPayment.setUpViews(AddPayment.java:188)
isMainActivity.settings.getCurrency()
It can't find static variable
settings
ofMainActivity
. I think the variable was cleared from memory.