0

I'm working on a very simple application that contains two activities - MainActivity (parent) and detailActivity (child). The parent and child relation is set in the manifest file.

Mainactivity has a recyclerView and for every item click in the recycler view, it open up the detailActivity.

Everything works so far except that my app runs slow.

I have a processJson method in the MainActivity's onCreate method that loads the data from a static json file. This file is pretty big( >700 rows)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_char);

    processJson();
    .......
}

Switching from MainActivity to DetailActivity

Intent intent = new Intent(this, CharDetailActivity.class);
intent.putExtra("charId", charId);
cxt.startActivity(intent);

Switching from DetailActivity to MainActivity is just through the backbutton or through the actionbar back button.

AndroidManifest.xml

    <activity
        android:name=".CharDetailActivity"
        android:label="@string/title_activity_char_detail" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".CharActivity" />
    </activity>

If I remove the meta-tags the app calls MainActivity's oncreate only once!

Everytime the app switches from DetailActivity to MainActivity, it calls onCreate and reloads the data. Is there a better way of loading data in the app ? or save instace of the MainActivity ?

Jayaram
  • 839
  • 1
  • 14
  • 24
  • Post the code where you switch from `DetailActivity` to `MainActivity` and vice-versa. – Simas Oct 12 '15 at 19:58
  • updated the question with code. Thanks – Jayaram Oct 12 '15 at 20:01
  • `onCreate()` shouldn't be called if you don't destroy `MainActivity` when switching to the other one. I wonder if you call `finish()` at any time, or return the main activity by invoking `startActivity`? – Simas Oct 12 '15 at 20:04
  • thats what I thought - but no, I do not call startActivity to return to MainActivity – Jayaram Oct 12 '15 at 20:06

2 Answers2

2

David says :

UP navigation launches the parent Activity with Intent.FLAG_ACTIVITY_CLEAR_TOP. The standard behaviour of this flag is to finish all Activities that are on top of the parent Activity in the task stack including the parent Activity itself and then launch a new instance of the parent Activity**. If you want to resume the existing instance of the parent Activity, then you can set the following in the manifest for the parent Activity:

android:launchMode="singleTop"

When CLEAR_TOP and SINGLE_TOP are used together, this will resume an existing instance of the parent Activity. In this case, onCreate() will not be called on the resumed parent Activity, but instead onNewIntent() will be called instead.

check his answer here

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
2

I would suggest looking at activity launch modes (http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en). You can use these to control whether or not your Main activity will recall onCreate(Bundle savedInstanceState) every time it is navigated to from the detail activity.

You will want to set the main activity's launch mode to either singleTop or singleTask depending on your use case.

Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30