简体   繁体   中英

Reload or Refresh Activity from OnClickListener not working

In my "ArticleActivity" (where the user reads the article), I have a list of "related articles". 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.

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.

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" ? 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.

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. 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. See Activity.launchmode for more info.

Do you really need 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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