简体   繁体   English

从OnClickListener重新加载或刷新活动不起作用

[英]Reload or Refresh Activity from OnClickListener not working

In my "ArticleActivity" (where the user reads the article), I have a list of "related articles". 在我的“ ArticleActivity”(用户在其中阅读文章)中,有一个“相关文章”列表。 When a user clicks one, it should reload or refresh the ArticleActivity to show the article they clicked on instead of the one they just read. 当用户单击一个文章时,它应该重新加载或刷新ArticleActivity以显示他们单击的文章,而不是他们刚刚阅读的文章。

I found many many answers online and have tried roughly 492 of them... I CAN get it to start a DIFFERENT activity, but I can't get it to restart the current one. 我在网上找到了很多答案,并尝试了大约492个答案...我可以让它开始一个不同的活动,但是我无法让它重新开始当前的活动。

My latest attempt: 我最近的尝试:

    //RELATED ARTICLE CLICK
    relatedArticleClickListener = new OnClickListener()
    {
        public void onClick(View v) {
            Log.d("MYLOG", "related article clicked " + v.getId());
            Intent myIntent = new Intent(v.getContext(), ArticleActivity.class);
            myIntent.putExtra("id", v.getId());
            startActivity(myIntent);
            finish();
        }   
    };

Update: 更新:

Could have it to do with the ArticleActivity having this: android:launchMode="singleTask" ? 可能与具有以下内容的ArticleActivity有关: android:launchMode="singleTask"吗? I need it there, but also need to be able to reload the activity with a new article. 我在那里需要它,但是还需要能够用新文章重新加载活动。

If I change ArticleActivity.class to MainActivity.class and leave all the rest of the code exactly the same, it DOES go to the MainActivity. 如果我将ArticleActivity.class更改为MainActivity.class并使所有其余代码完全相同,则它将转到MainActivity。

replace these lines 替换这些行

finish();
startActivity(myIntent);

with these 用这些

startActivity(myIntent);
finish();

After reading your update, and further understanding your question. 阅读您的更新后,进一步了解您的问题。 The behavior is related to your singleTask designation. 该行为与您的singleTask指定有关。 Under singleTask , if the activity already exists, it is not recreated, but instead its onNewIntent() method is invoked for you to handle the new intent. singleTask ,如果活动已经存在,则不会重新创建活动,而是会调用其onNewIntent()方法来处理新的意图。 See Activity.launchmode for more info. 有关更多信息,请参见Activity.launchmode

Do you really need singleTask ? 您真的需要singleTask吗? Because removing it should also cause your problem to go away. 因为删除它也会导致您的问题消失。 It will also allow your read articles to be in the back stack, so when your user is done with one article and presses the back key it will take them back to the first article. 它还将允许您阅读的文章位于后退堆栈中,因此当您的用户用完一篇文章并按向后键时,会将其带回到第一篇文章。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM