Search code examples
javaandroidandroid-intentandroid-activity

Why does my android apps activitie reopen when I close my app


I have been trying to solve this problem for a while now and I am sure there is a way to solve this error/problem but I haven't found that yet.

The problem I am having is once I exit the app via the back button or home button it closes but then reopens one of my activities which was opened before.

Do I need to close my activities once I press one of those buttons and how would I go around doing that.

Here is the code for my main activity that opens my other activity...

package com.togdem.tunnels;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class TunnelsActivity extends Activity
{

int vo = 2;
String mla = "";

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ImageView p_btn = (ImageView) findViewById(R.id.imageView1);

    p_btn.setOnTouchListener(new OnTouchListener()
    {

        public boolean onTouch(View view, MotionEvent event)
        {

        Intent myIntent2 = new Intent(view.getContext(), Cgt.class);

            startActivity(myIntent2);

            return false;

        }

    });

    SeekBar skb = (SeekBar) findViewById(R.id.seekBar1);

    try {

        InputStream in = openFileInput("sa.txt");
        if (in != null) {
            InputStreamReader input = new InputStreamReader(in);
            BufferedReader buffreader = new BufferedReader(input);

            mla = "";

            String line = new String();

            line = buffreader.readLine();

            mla += line;

            Toast msg = Toast.makeText(getBaseContext(), "Loaded Saved Data", Toast.LENGTH_LONG);
            msg.show();

            in.close();

            skb.setProgress(Integer.parseInt(mla.toString()));

        }
        else{}

    } catch(Exception e){}

    skb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {

        public void onProgressChanged(SeekBar event, int value, boolean sb)
        {

            vo = value;

        }

        public void onStartTrackingTouch(SeekBar event)
        {



        }

        public void onStopTrackingTouch(SeekBar event)
        {

            try
            {

                OutputStreamWriter out = new OutputStreamWriter(openFileOutput("sa.txt", 0));
                out.write(Integer.toString(vo));
                out.close();

            } catch (IOException e)
            {



            }

            Toast msg = Toast.makeText(getBaseContext(), "Set Sensitivity to: " + Integer.toString(vo), Toast.LENGTH_LONG);
            msg.show();

        }

    });

}

}

And here is the code for the activity I am trying to open...

package com.togdem.tunnels;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Cgt extends Activity
{

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView( R.layout.cgame );

    ImageView btn1 = (ImageView) findViewById(R.id.btn_c);

    ImageView btn2 = (ImageView) findViewById(R.id.btn_h);

    btn1.setOnTouchListener(new OnTouchListener()
    {

        public boolean onTouch(View view, MotionEvent event)
        {

            try
            {
                OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));

                out.write("4");
                out.close();

            } catch (java.io.IOException e) {}

            Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);

            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

            startActivity(myIntent);

            finish();

            return false;

        }

    });

    btn2.setOnTouchListener(new OnTouchListener()
    {

        public boolean onTouch(View view, MotionEvent event)
        {

            try
            {
                OutputStreamWriter out = new OutputStreamWriter(openFileOutput("spd.txt", 0));

                out.write("6");
                out.close();

            } catch (java.io.IOException e) {}

            Intent myIntent = new Intent(view.getContext(), TunnelsSpA.class);

            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

            startActivity(myIntent);

            finish();

            return false;

        }

    });

}

public boolean onKeyDown(int KeyCode, KeyEvent event)
{

    if (KeyCode == KeyEvent.KEYCODE_BACK);
    {

        onStop();

    }

    if (KeyCode == KeyEvent.KEYCODE_HOME);
    {

        onStop();

    }

    return super.onKeyDown(KeyCode, event);

}

protected void onStop()
{

    finish();
    Log.d("Stopping...", "Stopping the activity");

}

}

Thanks for any help you can give me,


Solution

  • You seem to have trouble with some basic Android behaviour.

    First of all, when the user presses the HOME button, he isn't exiting your application, he is just putting your application in the background so that he can go do something else. He doesn't expect your application to exit if he does this. In fact, when he returns to your application he expects to find it in exactly the same condition as when he left it. This is standard Android behaviour and you shouldn't change that unless you have a really good reason to.

    Secondly, when the user presses the BACK button, Android automatically finishes the current activity and returns to the previous activity in the activity stack. This is standard Android behaviour and you don't need to write anything special to make this happen.

    From looking at your code you have activity TunnelsActivity that starts activity Cgt. TunnelsActivity does not call finish() when it starts Cgt so the activity stack will have TunnelsActivity on the bottom and then Cgt on the top when that activity is running. in Cgt you are catching the BACK button and the HOME button and calling onStop(). This is definitely wrong. You should never call onStop(). onStop() is called by Android as part of the Activity lifecycle in certain situations. Calling onStop() will not make your activity go away nor will it make your application exit. All it will do is confuse Android. Also, when Android calls onStop() it expects the code to call through to super.onStop() which your code isn't doing. This will cause the code to throw an Exception, which may be why you are seeing your activities getting restarted.

    I suggest removing your onStop() method and removing your onKeyDown() method. That may solve all your problems.