简体   繁体   English

为什么当我关闭我的应用程序时,我的 android 应用程序活动会重新打开

[英]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,感谢你给与我的帮助,

Your Application is in Stopped State ...where the Activity (Your app Activity) is hidden by Another ForeGround Activity...您的应用程序处于Stopped State ......活动(您的应用活动)被另一个前台活动隐藏......

Either System kills this Activity when there is a need of memory .. or you can kill it by calling finish() or finishActivity()需要内存 System终止此Activity .. 或者您可以通过调用finish()finishActivity()

You seem to have trouble with some basic Android behaviour.您似乎对一些基本的 Android 行为有问题。

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.首先,当用户按下 HOME 按钮时,他并没有退出您的应用程序,他只是将您的应用程序置于后台,以便他可以做其他事情。 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.这是标准的 Android 行为,除非您有充分的理由,否则不应更改它。

Secondly, when the user presses the BACK button, Android automatically finishes the current activity and returns to the previous activity in the activity stack.其次,当用户按下 BACK 按钮时,Android 会自动完成当前的 Activity 并返回到 Activity 堆栈中的上一个 Activity。 This is standard Android behaviour and you don't need to write anything special to make this happen.这是标准的 Android 行为,您无需编写任何特殊内容即可实现此目的。

From looking at your code you have activity TunnelsActivity that starts activity Cgt .通过查看您的代码,您有活动TunnelsActivity启动活动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. TunnelsActivity不调用finish()当它开始Cgt所以活动堆栈将有TunnelsActivity底部,然后Cgt上时活动运行的顶部。 in Cgt you are catching the BACK button and the HOME button and calling onStop() .Cgt您正在捕捉 BACK 按钮和 HOME 按钮并调用onStop() This is definitely wrong.这绝对是错误的。 You should never call onStop() .你永远不应该调用onStop() onStop() is called by Android as part of the Activity lifecycle in certain situations.在某些情况下,作为 Activity 生命周期的一部分,Android 会调用onStop() Calling onStop() will not make your activity go away nor will it make your application exit.调用onStop()不会使您的活动消失,也不会使您的应用程序退出。 All it will do is confuse Android.它只会混淆Android。 Also, when Android calls onStop() it expects the code to call through to super.onStop() which your code isn't doing.此外,当 Android 调用onStop()它希望代码调用super.onStop() ,而您的代码没有这样做。 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.我建议删除您的onStop()方法并删除您的onKeyDown()方法。 That may solve all your problems.那可能会解决你所有的问题。

just use for quit the application System.exit(0);仅用于退出应用程序 System.exit(0);

not use finish();不使用完成();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我将其移动到后台时,为什么我的Android应用程序停止? - Why does my Android app stops, when I move it to the background? 当我尝试访问捆绑软件中的信息时,为什么我的Android活动被强制关闭? - Why does my Android activity force close when I try to access info I put in the bundle? 为什么关闭应用后我的应用会停止工作? - Why does my app stop working after I close it? 在更新方法中设置setText时,为什么我的应用程序强制关闭? - Why does my app force close when I setText in an update method? 为什么我的Android应用程序强制关闭 - why my android app force close 当我将 X 推到关闭窗口时,为什么我的 AWT Java 窗口没有关闭? - Why does my AWT Java Window not close, when I push X to the close window? 当我在活动开始时点击垃圾邮件时,为什么我的 Android Studio 应用程序会崩溃? - Why does my Android studio App crash when I spam click at the beginning of an activity? 如果我使用服务android,为什么我的应用程序关闭? - Why does my app shut down if i use service android? Android教程-为什么当我尝试访问现有的文本视图时我的应用程序崩溃 - Android Tutorials — Why does my app crash when I try to access an existing Text View 在AVD上运行时,为什么我的应用程序会自动关闭? - Why Does my app Closing Automatically when i run on AVD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM