Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用“后退”按钮重新启动上一个活动_Android_Android Activity_Back Button - Fatal编程技术网

Android 使用“后退”按钮重新启动上一个活动

Android 使用“后退”按钮重新启动上一个活动,android,android-activity,back-button,Android,Android Activity,Back Button,有两项活动。以下是第一个活动 @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); final CharSequence[] items = {"Pick from gallery", "Shoot now"}; //Prepare the list dialog box

有两项活动。以下是第一个活动

@Override
public void onCreate(Bundle savedInstanceState) {

    try {
        super.onCreate(savedInstanceState);

        final CharSequence[] items = {"Pick from gallery", "Shoot now"};

        //Prepare the list dialog box
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        //Set its title
        builder.setTitle("Choose a video");

        //Set the list items along with checkbox and assign with the click listener
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

            // Click listener
            public void onClick(DialogInterface dialog, int item) {

                if(items[item]=="Pick from gallery"){
                    Toast.makeText(getApplicationContext(), "Choose a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 1);
                    startActivity(r1);
                }

                if(items[item]=="Shoot now"){
                    Toast.makeText(getApplicationContext(), "Record a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 2);
                    startActivity(r1);
                }    
            }
        });

        AlertDialog alert = builder.create();

        //display dialog box
        alert.show();
    } 
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), "Unknown issue, Kindly restart the application", Toast.LENGTH_LONG).show(); 

        e.printStackTrace();
    }
当用户处于第二个活动中并按下“后退”按钮时,我希望第一个活动开始,但此时仅显示一个黑屏,弹出菜单不可见。我必须转动手机才能拿到菜单。默认情况下如何设置?另外,是否需要为
super.onCreate
使用
try

编辑
还有一个问题是,在第二个活动中,我做了很多任务,第一个任务是从多媒体资料中挑选视频。如果在此阶段按下
BACK
,则会出现一个空白屏幕,我必须再次按下
BACK
。如何避免这种情况?

尝试编写
alert.show()onResume()方法中编码>,而不是在onCreate()中编码>

当用户从第二个活动按下后退按钮时,将调用第一个活动的onResume()

@Override
public void onCreate(Bundle savedInstanceState) {

    try {
        super.onCreate(savedInstanceState);

        final CharSequence[] items = {"Pick from gallery", "Shoot now"};

        //Prepare the list dialog box
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        //Set its title
        builder.setTitle("Choose a video");

        //Set the list items along with checkbox and assign with the click listener
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

            // Click listener
            public void onClick(DialogInterface dialog, int item) {

                if(items[item]=="Pick from gallery"){
                    Toast.makeText(getApplicationContext(), "Choose a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 1);
                    startActivity(r1);
                }

                if(items[item]=="Shoot now"){
                    Toast.makeText(getApplicationContext(), "Record a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 2);
                    startActivity(r1);
                }    
            }
        });

        AlertDialog alert = builder.create();

        //display dialog box
        alert.show();
    } 
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), "Unknown issue, Kindly restart the application", Toast.LENGTH_LONG).show(); 

        e.printStackTrace();
    }

希望这有帮助

尝试编写
alert.show()onResume()方法中编码>,而不是在onCreate()中编码>

当用户从第二个活动按下后退按钮时,将调用第一个活动的onResume()

@Override
public void onCreate(Bundle savedInstanceState) {

    try {
        super.onCreate(savedInstanceState);

        final CharSequence[] items = {"Pick from gallery", "Shoot now"};

        //Prepare the list dialog box
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        //Set its title
        builder.setTitle("Choose a video");

        //Set the list items along with checkbox and assign with the click listener
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

            // Click listener
            public void onClick(DialogInterface dialog, int item) {

                if(items[item]=="Pick from gallery"){
                    Toast.makeText(getApplicationContext(), "Choose a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 1);
                    startActivity(r1);
                }

                if(items[item]=="Shoot now"){
                    Toast.makeText(getApplicationContext(), "Record a video", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Intent r1= new Intent("com.asin.amit.PIC");
                    r1.putExtra("choice", 2);
                    startActivity(r1);
                }    
            }
        });

        AlertDialog alert = builder.create();

        //display dialog box
        alert.show();
    } 
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), "Unknown issue, Kindly restart the application", Toast.LENGTH_LONG).show(); 

        e.printStackTrace();
    }

希望这有帮助

U可以使用此方法回调活动

这是我在应用程序中使用的一个示例

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // _progressDialog.dismiss();
            if (getIntent().getExtras().getString("From").equals("Menu")) {
                Intent intent = new Intent(getApplicationContext(),
                        VideoTypes.class);
                intent.putExtra("From",
                        getIntent().getExtras().getString("From"));
                startActivity(intent);
                finish();
            }

        }
        return super.onKeyDown(keyCode, event);
    }

我希望这将有助于你

你可以使用此方法回拨活动

这是我在应用程序中使用的一个示例

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // _progressDialog.dismiss();
            if (getIntent().getExtras().getString("From").equals("Menu")) {
                Intent intent = new Intent(getApplicationContext(),
                        VideoTypes.class);
                intent.putExtra("From",
                        getIntent().getExtras().getString("From"));
                startActivity(intent);
                finish();
            }

        }
        return super.onKeyDown(keyCode, event);
    }
我希望这将有助于u

super。onCreate()
是必需的。如果你不调用它,应用程序将崩溃

您可以使用,而不是
startActivity()
。因此,您可以在第二个活动完成时捕获事件,然后再次显示菜单。

super.onCreate()
是必需的。如果你不调用它,应用程序将崩溃


您可以使用,而不是
startActivity()
。因此,您可以在第二个活动完成时捕获事件,然后再次显示菜单。

您是否在第一个活动中的任何位置调用
finish()
?只需在活动中添加onResume方法,并将弹出菜单的代码放入onResume中即可method@GAMA我没有使用
finish
您正在呼叫
finish()
第一个活动中的任何位置?只需在活动中添加onResume方法,并将弹出菜单的代码放入onResume即可method@GAMA我没有使用
finish