Using intent between activities, in reverse - Android

1.1k Views Asked by At

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.

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.

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)

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.)

2

There are 2 best solutions below

0
On BEST ANSWER

Source: Getting a Result from an Activity

From Activity1:

public static final String RESULT_REQUEST = 1;

When you want to start Activity2:

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

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");
    }
    }
}

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();

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.

0
On

Put the value in the intent

Intent i = new Intent(SecondScreen.this, FirstScreen.class);   
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value:

 Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }