反向使用活动之间的意图-Android [英] Using intent between activities, in reverse - Android

查看:88
本文介绍了反向使用活动之间的意图-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android编程(以及一般的Java)很陌生,遇到了困扰我的问题.我正在构建一个基本的应用程序(用于实践),该应用程序可以访问已经构建的网站www.shodan.io.据我所知,这个特定的网站还没有android应用,我很喜欢我的研究. 因此,我有一个包含4个活动的应用程序,所有这些活动的加载情况都非常好(在堆栈论坛的大力帮助下).它们是:LoadingPage,MainActivity,SearchPage和ExploitSearch.在MainActivity上,我有一个Web视图来显示不同的页面,可在它们之间切换的按钮,徽标等.我的搜索和漏洞利用按钮每个都加载相应的页面而不会出现问题.现在是我的问题.

I am quite new to android programming (aswell as java in general) and have run into an issue that has me stumped. I am building a basic app (for practice) that accesses an already built website www.shodan.io . As far as i know there is no android app for this particular site yet and i quite enjoy it for my research. So I have an app that has 4 activities, all of which load beautifully (after much help from stack forums). They are: LoadingPage, MainActivity, SearchPage, and ExploitSearch. On my MainActivity, I have a webview to display the different pages, buttons to switch between, logos, etc. My search and exploit buttons each load the respective pages without issue. Now for my question/problem.

我想让搜索和利用活动获取输入(这是目前唯一的目的),然后将所述输入保存到字符串中,然后可以从MainActivty中访问该字符串,并将其用作我的url的搜索查询.

I would like to have the search and exploit activities take input (which is currently their only purpose), save said input into a string, which is then accessible from my MainActivty and used as the search query for my url.

到目前为止,我的搜索已经出现了很多论坛,它们在讲述如何从活动1中获取数据,并使用意图"使活动2读取数据.但是,我似乎找不到任何资源来进行反向操作或将其另存为字符串(也许在临时文件中,最好是以后可以调用的内容……有点像"input" + \ n,所以它不是覆盖)

My search thus far has turned up many forums telling how to take data from activity1 and have it readable by activity2 using "intent". However, I cant seem to find any resources for doing the reverse, or for saving it as a string (maybe in a temp file, and preferably something that can be recalled later... kinda like "input"+\n so its not overwritten)

谢谢您的时间 (ps.让我知道所需的任何代码部分,因为我不想用4个活动及其各自的布局文件填充此线程.)

Thank you for your time (ps. let me know any portions of code needed as i dont want to fill this thread with 4 activities and their respective layout files.)

推荐答案

来源:从活动中获取结果

来自活动1:

public static final String RESULT_REQUEST = 1;

要启动Activity2时:

When you want to start Activity2:

Intent intent = new Intent(this, Activity2.class);
startActivityForResult(intent, RESULT_REQUEST);

Activity2完成后将调用此名称:

This will be called when Activity2 is finished:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == RESULT_REQUEST) {
    // Make sure the request was successful
    if (resultCode == RESULT_OK) {
        String result = data.getStringExtra("result");
    }
    }
}

和Activity2,当您完成并想将字符串发送回去时:

And Activity2, when you are done and want to send the string back:

    Intent returnIntent = new Intent();
    returnIntent.putExtra("result","some string");
    setResult(RESULT_OK,returnIntent);
    finish();

我们已使用RESULT_REQUEST来唯一标识我们自己的REQUEST,并且您需要检查requestCode是否与您启动第二个活动的请求代码相同.

We have used the RESULT_REQUEST to uniquely identify our own REQUEST, and you do need to check if the requestCode is the same with the one you have started the second activity.

这篇关于反向使用活动之间的意图-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆