Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:41:53+00:00 2026-06-11T01:41:53+00:00

I have been trying to solve this problem for a while now and I

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:41:55+00:00Added an answer on June 11, 2026 at 1:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to solve this problem for a while, but couldn't with
I have been trying to solve this problem for a while, could not find
I’ve been trying to solve this problem for a while now to no avail.
I've been trying to solve this problem for a while now, and I'm at
I have spent quite a while trying to solve this problem, but to no
I've been trying to solve this problem for a couple of hours but I
I have been using PHP for a while, but now I am trying to
I have been trying to solve this strange problem with a website I am
I have been trying to solve this for a while. I am trying to
I have been trying to find ways to solve the problem. Firebug said syntax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.