-1

so I have an application with multiple activities. So when I switch from 1 activity to another and then click on the back button it works fine. But when I click on 1 activity then go out of the app and go back, it removes the main activity from memory so when you click on the back button, nothing happens. There are 2 possible solutions here, if there is somehow someway to detect if an activity is out of memory or if I can save the activity in memory. This is how I open the activity:

Intent intent = new Intent(context, QuoteActivity.class);
Gson gson = new Gson();
String quoteDataAsAString = gson.toJson(quoteData);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("QuoteData", quoteDataAsAString);
context.startActivity(intent);

And these are the 2 activities:

<activity
    android:name=".MainActivity"
    android:launchMode="singleInstance"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".quote.activity.QuoteActivity"
    android:launchMode="singleInstance"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
<activity
        android:name=".user.UserFeedBackActivity"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

This is what I have in the main view:

@Override
public void onStart() {
    super.onStart();
    if (QuoteActivity.isChangedWithFavourites()) {
        gridView.setAdapter(new QuoteImageAdapater(getApplicationContext(), reader.getCorrectData(currentSection), section));
        QuoteActivity.setChangedWithFavourites(false);
    }
}

A gif showing it: https://i.imgur.com/YZIkBU1.gifv

And this is how it should be: https://i.imgur.com/hghjB9L.gifv

This problem occurs on both activities even when I don't call the finish method.

Edit: I figured it out I had to change launchMode to singleTop

Kiraged
  • 519
  • 2
  • 9
  • 28

1 Answers1

0

In Application class, You can override onLowMemory() method.

The documentation says,

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process has been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

When OnLowMemory is called, Using EventBus you can trigger an event which should be caught by your Activity. Upon which you can perform your desired action.

If you looking to find, If the Activity has encountered OutOfMemoryException(OOM), then follow this answer.

How do I discover memory usage of my application in Android?

Pavan
  • 519
  • 1
  • 6
  • 16
  • That's not it but thank you for future reference. I have updated my post to give a better look at what I mean. – Kiraged Apr 09 '18 at 18:43