gpt4 book ai didi

java - 防止再次启动启动器 Activity (如果已启动),只需恢复它即可

转载 作者:行者123 更新时间:2023-12-02 02:41:03 24 4
gpt4 key购买 nike

我正在从事 Android Activity 。我有一项主要 Activity 和另外两项 Activity ,这两项 Activity 是从主要 Activity 启动的。当启动这两个 Activity 中的任何一个时,每个 Activity 都有后退按钮,按下后退按钮时,我希望 Intent 启动恢复的主要 Activity ,而不是在其他页面上重新启动它。下面是主要 Activity 的代码

package com.example.nadeemahmad.smartcalculator;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends Activity {

Button show_cam_ctrl,
show_voice_ctrl;

TextView ma_res_txt;

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

//Control Buttons
show_cam_ctrl = (Button) findViewById(R.id.show_cam_ctrl);
show_voice_ctrl = (Button) findViewById(R.id.show_voice_ctrl);

show_cam_ctrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,cam_calculator.class);
startActivity(i);
}
});
show_voice_ctrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,voice_calculator.class);
startActivity(i);
}
});


}

两项 Activity 的代码

public class voice_calculator extends Activity {

Button back_frm_voice;

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

back_frm_voice = (Button) findViewById(R.id.back_frm_voice);

back_frm_voice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(voice_calculator.this,MainActivity.class);
startActivity(i);
finish();
}
});
}
}
public class cam_calculator extends Activity {

Fragment cam_fragment;
Button back_frm_came;

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

back_frm_came = (Button) findViewById(R.id.back_frm_came);

back_frm_came.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(cam_calculator.this,MainActivity.class);
startActivity(i);
finish();
}
});

}
}
这是主要 Activity ,BTN1 和 BTN2 顶部有两个按钮 enter image description here 第二个 Activity 是在 BTN1 按下时启动的,但是当我按下顶部的后退按钮时 enter image description here 此主要 Activity 已启动,但不是恢复的 Activity ,当我按手机上的后退按钮时,它会关闭,屏幕上会出现带有计算的主要 Activity ,我想要的是,当我按后退按钮时,所以 Intent 应该带我进入主要 Activity 并恢复计算。 enter image description here

最佳答案

只需从 OnClickListener 调用 cam_calculator.this.finish()voice_calculator.this.finish() 即可。这些 Activity 将完成并“自动”返回到 MainActivity。

编辑:

如果您输入如下代码:startActivity(new Intent(this, SomeActivity.class));,您将直接告诉框架启动一个 Activity 。只需删除它即可!

关于java - 防止再次启动启动器 Activity (如果已启动),只需恢复它即可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45468056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com