Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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 - Fatal编程技术网

Android 刷新活动并重新打开

Android 刷新活动并重新打开,android,android-activity,Android,Android Activity,这是我的第一篇帖子,所以请友好一点:) 我有一个问题,在我看到的帖子里没有人给出答案 我的应用程序有一个列表和一个按钮来打开一个活动,此活动将创建一个新项目,在按下“创建”按钮时显示在上一个活动的列表中 我该怎么做 这是我为第一个按钮编写的代码: intent = new Intent(this.getBaseContext(), NewTestActivity.class); this.finish(); startActivity(intent); 这是返回刷新的代码: Intent in

这是我的第一篇帖子,所以请友好一点:) 我有一个问题,在我看到的帖子里没有人给出答案

我的应用程序有一个列表和一个按钮来打开一个活动,此活动将创建一个新项目,在按下“创建”按钮时显示在上一个活动的列表中

我该怎么做

这是我为第一个按钮编写的代码:

intent = new Intent(this.getBaseContext(), NewTestActivity.class);
this.finish();
startActivity(intent);
这是返回刷新的代码:

Intent intent = new Intent(getBaseContext(), TestListActivity.class);                       
startActivity(intent);
但是goback的代码很有用,因为活动不会刷新。 我必须以不同的方式来调用新活动?还是以不同的方式回到previus活动?或者当我回到previus活动时,正常返回并刷新活动

好了,就这些。 对不起,我的英语不好,如果这个问题在另一个帖子中得到了回答,请给我链接阅读,因为我找不到:)

PS:我从12月份开始使用android

谢谢你的帮助

  • 当你要开始新的活动时,在你真正需要这种行为之前,你不应该关闭当前的活动。(从代码中删除
    this.finish();
    行)
  • 此外,在实际需要活动之前,不应手动关闭活动。当用户按下设备上的“后退”按钮时,Android会从“后退堆栈”中弹出上一个活动。再次阅读文档
  • 根据您的描述,您有一个元素列表。因此,为了刷新列表,您需要更新数据集并通过
    ListView.notifyDataSetChanged()
    方法调用通知列表

  • 在得到真正的答案之前,我想展示一下对代码的一些改进。 首先,当从活动创建意图时(或当您通常需要上下文时),无需调用
    getBaseContext()
    ,您只需使用
    this

    intent = new Intent(this, NewTestActivity.class);
    
    其次,android擅长处理活动,您不必使用
    finish()
    手动关闭第一个活动。Android将自动暂停或停止您的第一个活动,并在您返回时将其恢复

    第三,在您的情况下,您可能希望使用
    startActivityForResult()
    而不是
    startActivity()
    ,原因我将在下面解释。
    这将使您的代码如下所示:

    private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value.
    intent = new Intent(this, NewTestActivity.class);
    startActivityForResult(intent, MY_REQUEST_CODE);
    
    现在,
    startActivityForResult()
    启动一个活动并等待该新活动的结果。当您在新活动中调用
    finsih()
    时,您将在第一个Activitys
    onActivityResult()
    -方法中结束,其中的数据来自现在已关闭的新活动:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return.
        if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data.
        //After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent:
        Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used.
        itemList.add(item); //This is the list that was specified in onCreate()
        //If you use an Adapter, this is the place to call notifyDataSetChanged();
    }
    
    为了让这一切都起作用,我们需要在第二个活动中做一些事情: 创建项目后,我们必须设置一个结果:

    //We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable)
    Item theCreatedItem; //This is what was created in the activity
    Intent data = new Intent();
    data.putSerializable(theCreatedItem);
    setResult(RESULT_OK, data);
    finish();
    

    如上所述,这应该返回到带有该项的第一个Activitys
    onActivityResult()
    -方法。

    onResume()方法中是否有代码?通常,后续活动调用将调用onResume。否,加载列表的操作在onCreate()方法中。尝试在onResume()中添加相同的内容,请参见。好的,在回答onResume()方法时,将该操作放在该方法中进行探测。我只是先尝试一下。嗯……解决方案很简单,我只想在另一个类的函数中将操作符private改为public。然后,在我想要的任何地方使用更新列表功能:)非常感谢您的帮助!哇…真的很明确…但是,我认为有一个最好的方法来做我想做的事。只需要在每次我展示时刷新活动…没什么奇怪的。真的谢谢Jave…你是一位了不起的老师!就像我说的,解决方案很简单,我只想在另一个类的函数中将操作符private改为public。然后在我想要的任何地方使用更新列表功能:)非常感谢您的帮助!