setResult in intent para parent Activity传递

时间:2015-01-07 14:38:12

标签: android

我不明白这段代码中发生了什么。需要帮助。

     Intent i = new Intent(getApplicationContext(),
                    RSSNewsReaderPBActivity.class);
            // send result code 100 to notify about product update
            setResult(100, i);
            startActivity(i);

以及为什么要在其中使用int值。

  This is method of the code 

  protected String doInBackground(String... args) {
        String url = args[0];
        rssFeed = rssParser.getRSSFeed(url);
        Log.d("rssFeed", " " + rssFeed);
        if (rssFeed != null) {
            Log.e("RSS URL",
                    rssFeed.getTitle() + "" + rssFeed.getLink() + ""
                            + rssFeed.getDescription() + ""
                            + rssFeed.getLanguage());
            RSSDatabaseHandler rssDb = new RSSDatabaseHandler(
                    getApplicationContext());
            WebSite site = new WebSite(rssFeed.getTitle(),
                    rssFeed.getLink(), rssFeed.getRSSLink(),
                    rssFeed.getDescription());
            rssDb.addSite(site);
            Intent i = new Intent(getApplicationContext(),
                    RSSNewsReaderPBActivity.class);
            // send result code 100 to notify about product update
            setResult(100, i);
            startActivity(i);
            return null;
        } else {
            runOnUiThread(new Runnable() {
                public void run() {
                    textViewMessage
                            .setText("Rss url not found. Please check the url or try again");
                }
            });
        }
        return null;
    }

1 个答案:

答案 0 :(得分:0)

在这种情况下,错误地使用了setResult:必须在完成活动之前调用setResult,它将通知父活动您当前的活动运行良好或者是否有问题(考虑到父活动已启动新活动)一个有startActivityForResult)

假设您有一个将启动ActivityTwo的ActivityOne:

ActivityOne:

Intent intent = new Intent(this, ActivityTwo.class);
startActivityForResult(intent, MY_REQUEST_CODE);
在您想要关闭活动时,在ActivityTwo中

if(everything_is_ok)
    setResult(1);
else
    setResult(0);
finish();

在ActivityOne中,您将收到有关ActivityTwo已完成的通知:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == MY_REQUEST_CODE)
    {
        if(resultCode == 1)
            //everything went fine
        else
            //something went wrong
    }
}

希望这有帮助!