Is there something that needs to be added to avoid memory leaks when switching between activities?

I'm trying to figure out how to have multiple activities with my application. I have striped it down to the lowest element to make it easy to see what I'm doing and understand my question.

When the program runs it shows a screen (activity) that says “Multiple Activities Example (screen1). It has a “Button 1” to click to see the next activity.

Clicking the Button 1 will take you to a different activity screen that says “Multiple Activities Example (screen2). It has a button 2 that when pressed will go back to screen1.

I was trying to figure out some type of return method to go back to the previous screen, but couldn't figure out any.

So, my concern/question is, is there some type of memory leak where using this method is going deeper into layers that require some type of return?

The code is attached.

MainActivity:

package apollo.MultipleActivities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void button1_click(View v) {
        Intent myIntent = new Intent(this, SecondActivity.class);
        startActivity(myIntent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Second Activity:

package apollo.MultipleActivities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
    }

    public void button2_click(View v) {
        Intent myIntent = new Intent(this, MainActivity.class);
        startActivity(myIntent);
    }
}

Main Activity XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/screen1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" android:onClick="button1_click"/>

</LinearLayout>

Second Activity XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/screen2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button2_click"
        android:text="@string/button2" />

</LinearLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="apollo.MultipleActivities"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="apollo.MultipleActivities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>        
    </application>

</manifest>

The app runs perfect and do exactly what I'm trying to have it do. My concern is about the integrity of the overall Android. If the program stays running and is used a lot, am I in for memory leak crashes down the road.

Thanks!

Answers


I was trying to figure out some type of return method to go back to the previous screen, but couldn't figure out any.

Option #1: Call finish() instead of startActivity() in SecondActivity. This will destroy the current activity and return control to the preceding activity, which in this case is MainActivity.

Option #2: Add FLAG_ACTIVITY_REORDER_TO_FRONT on the Intent you use to start MainActivity from SecondActivity to bring the existing instance of MainActivity to the top of the back stack. Pressing BACK from there would return you to SecondActivity, which may not be what you have in mind.


Need Your Help

Non-nullable property is null

c#

I have a very simple class Address:

Javascript alert from JSON response

javascript mysql json alert

I'm trying to build an app that gets data from server with getJSON and then alerts the row. This is the code I use but it doesn't show the row from JSON, it just says "undefined". What could be the