Send and receive from Activity at the same time - android

I have 2 Activities.
When I click the Button in board Activity, main will open and execute, then send value (mark) to the board Activity
It works.
But I want also to send 3 arrays from the board Activity to the main Activity, as variables, according which Button is clicked (I have 3 Buttons in the board Activity, each array for Button).
This is the function I used in board Activity:
public void getMessage(View V) {
// Create The Intent and Start The Activity to get The message
Intent intentGetMessage = new Intent(this, MainActivity.class);
startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
}
// Call Back method to get the Message form other Activity override the method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 2) {
// fetch the message String
String message = data.getStringExtra("MESSAGE");
// Set the message string in textView
mm.setText(message);
and this is the submit function in the main Activity :
public void submitMessage(View V) {
String message = String.valueOf(mark);
Intent intentMessage = new Intent();
// put the message to return as result in Intent
intentMessage.putExtra("MESSAGE", message);
// Set The Result in Intent
setResult(2, intentMessage);
// finish The activity
finish();
}

The simplest way is to use a singleton, that is a class of which only one instance exists. And the simplest way to do that is to define a class containing three static arrays, with public (static) accessors. You can then reference this class from either activity, place arrays in from one and read them back from the other. For example,
public class SampleSingleton {
private static String[] _array1;
private static Integer[] _array2;
private static boolean[] _array3;
public static String[] get_array1() {
return _array1;
}
public static void set_array1(String[] _array1) {
SampleSingleton._array1 = _array1;
}
public static Integer[] get_array2() {
return _array2;
}
public static void set_array2(Integer[] _array2) {
SampleSingleton._array2 = _array2;
}
public static boolean[] get_array3() {
return _array3;
}
public static void set_array3(boolean[] _array3) {
SampleSingleton._array3 = _array3;
}
}
There are other more elegant, and complicated, methods and the answers to this question give a few pointers, but as a quick and simple approach this'll do.

Related

How to get data from fragment to parent activity & pass it to back activity?

How flow goes?
Activity 1 -----> Activity 2 (containing/inside) ------> Fragment
WhatI want to achieve?
Fragment (sends some data back to Activity 2) ----> Activity 2 (onBackPressed : collects that data & send it back to Activity 1) ---> Activity 1
How should I achieve above. I really don't want to use any variables/constants to cache the fragment data. Need to know any in-built method to handle this?
Moreover,
Activity 2 loads Fragment inside it.
In onBackPressed, I'm using setResult in Activity 2 to do standard data passing using startActivityForResult from Activity 1.
Also, if I write any method inside Fragment & call from Activity 2 using then due to that to/fro process a WHITE screen appears. So, really don't want to write own method & need to manage it while leaving the Fragment.
You should start Activity2 with startActivityForResult as below;
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, requestCode);
And in Activity2/fragment, you should finish acitivity as below;
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish()
And get result in Activity1 as below;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (this.requestCode == requestCode) {
if(resultCode == Activity.RESULT_OK){
//Get result
}
}
}
Hope it helps.
To pass data from Activity 2 to Activity 1 you can use startActivityForResult() in Activity 1 to start Activity 2, and onActivityResult() in Activity 1 to receive that data.
To pass data from Fragment to Activity 2, I'd suggest to use some interface like
interface OnSomeDataListener {
void onSomeData(SomeData data);
}
Then implement a setter method for this interface to a fragment like
public void setOnSomeDataListener(OnSomeDataListener listener) {
this.listener = listener;
}
And then in Activity, when instantiating a Fragment:
YourFragment fragment = new YourFragment();
fragment.setOnSomeDataListener(new OnSomeDataListener() {
void onSomeData(SomeData data) {
//return the result
Intent intent = new Intent();
intent.putExtra("data", data);
setResult(RESULT_OK, intent);
finish();
}
}
And finally, in the fragment, when you want to return some data to Activity:
if(listener != null) {
listener.onSomeData(dataToReturn);
}
From your FirstActivity call the SecondActivity using startActivityForResult() method
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your SecondActivity set the data which you want to return back to FirstActivity onBackPressed of SecondActivity.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("bool",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
Now in your FirstActivity class write following code for the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
boolean bool = data.getBooleanExtra("bool");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//
To send data from Fragment to Second activity, you can use interface callback
public interface sendDataListener
{
void sendData(boolean foo);
}
Implement this listener to Second activity
Try to do this:
public class MyFragment extends Fragment{
private MyFragmentCommunicator myFragmentCommunicator;
....
myFragmentCommunicator.sendDataToActivity(name, image, isSend);
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
myFragmentCommunicator = (MyFragmentCommunicator)activity;
}
}
then declare your interface:
public interface MyFragmentCommunicator{
void sendDataToActivity(String name, String image, boolean isSend);
}
and then in your Activity do this:
public class MyActivity extends AppCompatActivity implements MyFragmentCommunicator{
#Override
public void sendDataToActivity(String name, String image, String price) {
//Manipulate the data
}
}
Hope it helps!!!
i acheived in Following way
In Activity write setters and getters
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
and in Fragment
filePath = ((YourActivity) getActivity()).getFilePath();
fileName=((YourActivity) getActivity()).getFileName();
if You are Using Same Fragment in More Than 1 Activity You can Create a constructor for that fragment and Pass context and check context is of which activity
public class BookmarkFragment extends Fragment {
private String filePath;
private String fileName;
Context contextCheckClass;
public BookmarkFragment(Context ctx ) {
this.contextCheckClass=ctx;
}
#SuppressLint("InflateParams")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
group= (View)inflater.inflate(R.layout.fragment_bookmark, null);
if(contextCheckClass instanceof FirstReaderScreen){
filePath = ((FirstReaderScreen) getActivity()).getFilePath();
fileName=((FirstReaderScreen) getActivity()).getFileName();
// ispurchased=((FirstReaderScreen) getActivity()).isIspurchased();
}
else if(contextCheckClass instanceof MainReaderScreen){
filePath = ((MainReaderScreen) getActivity()).getFilePath();
fileName=((MainReaderScreen) getActivity()).getFileName();
// ispurchased=((MainReaderScreen) getActivity()).isIspurchased();
}
return group;
}
for calling fragment
BookmarkFragment bookmarkFragment=new BookmarkFragment(FirstReaderScreen.this);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, bookmarkFragment)//LL_fragment is container
.addToBackStack(null)
.commit();

How I can return data from fragment to activity?

I have trouble to return data from fragment to activity.
I have Activity A: is main activity for add product, user when click the text view (selectCategory) this activity show to user activity B which content fragment Bb(all type of Category), when user clicks in some of this categories, the app going to show user activity C which has fragment Cc (subcategories).
Here my problem: I need when the user clicks in some of this subcategory which content in fragment Cc to return data to activity A then show to the user.
A call --> B
categoryTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i= new Intent(A.this,B.class);
i.putExtra("fragmenttype","CategoryFragment");
startActivityForResult(i,10);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
titleProduct.setText(data.getStringExtra("nodecategory"));
}
}
}
return data from Cc fragment to A but nothing happened (I need from code to go to activity A to catch data in onActivityResult):
#Override
public void onClick(TreeNode node, Object value) {
Intent i = new Intent();
i.putExtra("nodecategory","hi");
getActivity().setResult(Activity.RESULT_OK, i);
getActivity().finish();
}
I tend to rely heavily on EventBus on handling some case like yours. Using EventBus makes our life simpler. We don't need to clutter our code with interface here and there.
A ---> B
First, A will launch B with:
Intent i= new Intent(A.this, B.class);
i.putExtra("fragmenttype","CategoryFragment");
startActivity((i); // No need using startActivityForResult,
// instead receive it from Event generated by B.class.
Then in B, send Event whenever there is something that A need to catch:
EventBus.getDefault().post(new MyEvent("passed_data"));
// You can finish B activity if its job finished.
finish();
With MyEvent class like:
public class MyEvent {
private String data;
public MyEvent(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
In A, receive the Event:
#Subscribe
public void onEventMessage(MyEvent event) {
// process event here
mTextView.setText(event.getData);
}
For C --> A
Consider that we have show C activity. Whenever we want to inform A that something need to do based on C activities (something we do), we just need to fire Event with the data:
EventBus.getDefault().post(new DataFromCEvent("your_c_data"));
with DataFromCEvent like:
public class DataFromCEvent {
private String data;
public DataFromCEvent(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
In A, you can receive it by adding:
#Subscribe
public void onEventMessage(DataFromCEvent event) {
// process event here
mTextView.setText(event.getData);
}
This also works with fragment. We only need to fire an Event for some changes that we need in other activities.
use Intent.FLAG_ACTIVITY_FORWARD_RESULT on your activity B.
Ex-> ActivityA -> ActivityB -> ActivityC - > ActivityA
ActivityA:
Intent intentB = new Intent(this, ActivityB.class);
startActivityForResult(intentB, requestCode);
ActivityB: activity B will forward to
Intent intentC = new Intent(this, ActivityC.class);
intentC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(intentC);
finish();
ActivityC:
setResult(resultCode);
finish();

Use same instance of Activity

I am making a game, and i want to use the same Activity every time when a user fails the Level and i don't wanna create all the Views every time when the FailedActivity is shown.
For example i have Level1Activity:
public class Level1Activity extends Activity{
private static final int REQUEST_FAIL_ACTIVITY = 10;
private static final int RESULT_RETRY_LEVEL= 11;
public void onCreate(Bundle savedIntanceState){
...
}
public void showFailedActivity(){
Intent intent = new Intent(this, FailedActivity);
intent.putExtra("text1", "You failed! You made only 50 points");
intent.putExtra("text2", "Try again later");
startActivityForResult(intent, REQUEST_FAIL_ACTIVITY );
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_FAIL_ACTIVITY ){
if(resultCode == RESULT_BACK){
finish();
}else if(resultCode == RESULT_RETRY_LEVEL){
retryLevel();
}else{
finish();
}
}
}
}
And class FailedActivity
private RelativeLayout bgLayout;
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
private Button buttonRetryLevel;
private Button buttonBack;
public class FailedActivity extends Activity{
public void onCreate(Bundle saved){
super.onCreate(saved)
setContentView(R.layout.activity_failed);
bgLayout = (RelativeLayout)findViewById(R.id.bgLayout);
text1 = (TextView)findViewById(R.id.textView1);
...
buttonBack = (Button)findViewById(R.id.btnBack);
Bundle bundle = getIntent().getExtras();
text1.setText(bundle.getString("text1");
...
text5.setText(bundle.getString("text5);
}
public void buttonRetryClicked(View view){
setResult(Level1Activity.RESULT_RETRY_LEVEL);
finish();
}
...
}
So, i want to call findViewById for all the Views only once, and reuse the Activity because the FailedActivity i use for all my Levels, i need to pass only the Strings that will be shown, maybe the background color, etc.
My question is: can i create the FailedActivity only ones, and then when i call startActivityForResult just to pass the Bundle and the Views to be created only once.
You can use
singleInstance
as launch mode and override onNewIntent() function to bypass the re-creation of the activity UI components.
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
// handle parameters here.
}
Yes, this is possible as noted in the another answer. If you do this though, you are stepping outside of the standard Activity lifecycle. Generally, when an android app starts a new Activity, the old Activity is destroyed and a new instance of the new one is created. This also occurs when you change the device orientation (portrait -> landscape). The Android runtime does this for memory efficiency and because the view dimensions may have changed. Have a look at this if you want to learn more about the activity lifecycle.
Are the bundled values you're putting in an intent from your Level1Activity going to be the same for other levels? If so you could hardcode those strings directly into the xml layout file for FailedActivity. If you need all level activities to have the ability to open the FailedActivity, I'd consider creating a levelActivity superclass. This will simplify the FailedActivity creation code and stop you from having to repeat yourself. Something like this:
public abstract class LevelActivity extends Activity {
public void showFailedActivity(){
Intent intent = new Intent(this, FailedActivity);
intent.putExtra("text1", "You failed! You made only 50 points");
intent.putExtra("text2", "Try again later");
startActivityForResult(intent, REQUEST_FAIL_ACTIVITY );
}
}
public class Level1Activity extends LevelActivity {
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_FAIL_ACTIVITY ){
if(resultCode == RESULT_BACK){
finish();
}else if(resultCode == RESULT_RETRY_LEVEL){
super.retryLevel(); // calls LevelActivity.retryLevel()
}else{
finish();
}
}
}

How to call startactivityforresult from a non-activity class to get the resuts

Is it possible to call startActivityForResult() from a non-activity class to get the results?
Scenario is something like this:
I have a class NonActivity (it doesn't derive from Activity as its not a UI).
This class will have bunch of functions(steps basically) to run.
One of the steps requires to show UI(Activity) and then get the result (user enter something).
Then been able to use that data in next following steps.
How can this be achieved without deriving from activity class as I don't have UI component?
Also since I don't want to derive from activity class that means I cannot override OnActivityResult(). Where results actually come from?
startActivityForResult() is only available from real on-screen activities, since it is a method in, well, Activity. Please redesign your application so that the user interface is driven from activities.
On the other hand, if your non Activity class is initialized and used from an onscreen Activity, you could pass that instance of the Activity to your class as a parameter in the constructor and use it to launch other Activities.
Be careful though. Using this method increases the risk of a memory leak, as the external class (Utils in my example) might keep a reference to the Activity even after its gone.
If all you want to do is access data, then you could try writing it to SharedPreferences or a Database or some files and then using the application context (passed in via a constructor again) to read it. This reduces the risk of a memory leak. Something like:
MyApiClass myApiClass = new MyApiClass(getApplicationContext());
EXAMPLE CODE
Main Activity:
public class Main extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils util = new Utils(this);
util.startTest();
}
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
Toast.makeText(this, "onActivityResult called", Toast.LENGTH_LONG).show();
super.onActivityResult(arg0, arg1, arg2);
}
}
Utils class (which launches for result):
public class Utils {
Activity activity;
public Utils(Activity ac) {
activity = ac;
}
public void startTest() {
Intent i = new Intent(activity, Test.class);
activity.startActivityForResult(i, 1);
}
}
Test Activity:
public class Test extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
this.setResult(Activity.RESULT_OK);
this.finish();
}
}
StartActivityForResult from a class using a fragment with no visible GUI. You might find something like this in a utility class.
see runGetUserAccount below. It creates its own fragment and executes a startActivityForResult. Then it has it's own onActivityResult.
public class MyGooglePlay {
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 31502;
private ActionBarActivity activity;
private FragmentManager fragManager;
public MyGooglePlay(ActionBarActivity activity) {
this.activity = activity;
this.fragManager = activity.getSupportFragmentManager();
}
/**
* Starts an activity in Google Play Services so the user can pick an
* account
*/
private String mEmail = "";
static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
public void runGetUserAccount() {
if (TextUtils.isEmpty(mEmail)) {
// run this code in gui less fragment so we can pickup the
// on activity result from inside the mygoogleplay class.
Fragment f = new Fragment() {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
String[] accountTypes = new String[] { "com.google" };
Intent intent = AccountPicker.newChooseAccountIntent(null,
null, accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == Activity.RESULT_OK) {
set_Email(data
.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
// getUsername();
}
super.onActivityResult(requestCode, resultCode, data);
}
//this is to verify the fragment has been removed.
//you can log or put a breakpoint to verify
#Override public void onDestroy(){
super.onDestroy();
}
};
FragmentTransaction fragmentTransaction = this.fragManager
.beginTransaction();
fragmentTransaction.add(f, "getusername");
fragmentTransaction.commit();
}
}
/**
* #param mEmail
* the mEmail to set
*/
private void set_Email(String mEmail) {
this.mEmail = mEmail;
if (!TextUtils.isEmpty(mEmail)) {
// TODO notify caller email is ready;
// activity.onEmailReady(mEmail);
}
//we are done with the "getusername" fragment
Fragment f = fragManager.findFragmentByTag("getusername");
if (f!=null) {
fragManager.beginTransaction().remove(f).commit();
}
}
}
U should pass context as Activity,then u will get solution.
try this below code.it will work
In non Activity class
public class nonActivity {
public static void method(Activity activity)
{
Intent intent = new Intent(activity, SecondActivity.class);
activity. startActivityForResult(intent, REQUEST_CODE);
}
}
In SecondActivity
Intent intent = getIntent();
intent.putExtra("data", "data"); //here u can pass data to previous activity
setResult(RESULT_OK, intent);
finish();
In firstActivity
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String status = data.getStringExtra("data");
//Do what u want with data
}
} catch (Exception e) {
System.out.println("=====Exception=====" + e.toString());
}
}
If you want the result back from the activity to your normal class, supposed it is a class with a custom adapter within it.
you cannot use startActivityForResult because you are not in an activity
what I did is that i launched the activity from the class with an intent. Then I calculated or did what I have to. From this activity I send the information to the main class supposed with a method MainActivity.the_method() and in the main activity I changed the custom adapter o did what I have to using the adapter object and calling adapter.getItem(position)
Hope this can give you an idea

Sending data back to the Main Activity in Android

I have two activities: main activity and child activity.
When I press a button in the main activity, the child activity is launched.
Now I want to send some data back to the main screen. I used the Bundle class, but it is not working. It throws some runtime exceptions.
Is there any solution for this?
There are a couple of ways to achieve what you want, depending on the circumstances.
The most common scenario (which is what yours sounds like) is when a child Activity is used to get user input - such as choosing a contact from a list or entering data in a dialog box. In this case, you should use startActivityForResult to launch your child Activity.
This provides a pipeline for sending data back to the main Activity using setResult. The setResult method takes an int result value and an Intent that is passed back to the calling Activity.
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
resultIntent.putExtra("some_key", "String data");
setResult(Activity.RESULT_OK, resultIntent);
finish();
To access the returned data in the calling Activity override onActivityResult. The requestCode corresponds to the integer passed in the startActivityForResult call, while the resultCode and data Intent are returned from the child Activity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (MY_CHILD_ACTIVITY) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
String returnValue = data.getStringExtra("some_key");
}
break;
}
}
}
Activity 1 uses startActivityForResult:
startActivityForResult(ActivityTwo, ActivityTwoRequestCode);
Activity 2 is launched and you can perform the operation, to close the Activity do this:
Intent output = new Intent();
output.putExtra(ActivityOne.Number1Code, num1);
output.putExtra(ActivityOne.Number2Code, num2);
setResult(RESULT_OK, output);
finish();
Activity 1 - returning from the previous activity will call onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActivityTwoRequestCode && resultCode == RESULT_OK && data != null) {
num1 = data.getIntExtra(Number1Code);
num2 = data.getIntExtra(Number2Code);
}
}
UPDATE:
Answer to Seenu69's comment, In activity two,
int result = Integer.parse(EditText1.getText().toString())
+ Integer.parse(EditText2.getText().toString());
output.putExtra(ActivityOne.KEY_RESULT, result);
Then in activity one,
int result = data.getExtra(KEY_RESULT);
Sending Data Back
It helps me to see things in context. Here is a complete simple project for sending data back. Rather than providing the xml layout files, here is an image.
Main Activity
Start the Second Activity with startActivityForResult, providing it an arbitrary result code.
Override onActivityResult. This is called when the Second Activity finishes. You can make sure that it is actually the Second Activity by checking the request code. (This is useful when you are starting multiple different activities from the same main activity.)
Extract the data you got from the return Intent. The data is extracted using a key-value pair.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int SECOND_ACTIVITY_REQUEST_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// "Go to Second Activity" button click
public void onButtonClick(View view) {
// Start the SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);
}
// This method is called when the second activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Get String data from Intent
String returnString = data.getStringExtra("keyName");
// Set text view with string
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(returnString);
}
}
}
}
Second Activity
Put the data that you want to send back to the previous activity into an Intent. The data is stored in the Intent using a key-value pair.
Set the result to RESULT_OK and add the intent holding your data.
Call finish() to close the Second Activity.
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
// "Send text back" button click
public void onButtonClick(View view) {
// Get the text from the EditText
EditText editText = (EditText) findViewById(R.id.editText);
String stringToPassBack = editText.getText().toString();
// Put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.putExtra("keyName", stringToPassBack);
setResult(RESULT_OK, intent);
finish();
}
}
Other notes
If you are in a Fragment it won't know the meaning of RESULT_OK. Just use the full name: Activity.RESULT_OK.
See also
Fuller answer that includes passing data forward
Naming Conventions for the Key String
FirstActivity uses startActivityForResult:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, int requestCode); // suppose requestCode == 2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2)
{
String message=data.getStringExtra("MESSAGE");
}
}
On SecondActivity call setResult() onClick events or onBackPressed()
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(Activity.RESULT_OK, intent);
Call the child activity Intent using the startActivityForResult() method call
There is an example of this here:
http://developer.android.com/training/notepad/notepad-ex2.html
and in the "Returning a Result from a Screen" of this:
http://developer.android.com/guide/faq/commontasks.html#opennewscreen
UPDATE Mar. 2021
As in Activity v1.2.0 and Fragment v1.3.0, the new Activity Result APIs have been introduced.
The Activity Result APIs provide components for registering for a result, launching the result, and handling the result once it is dispatched by the system.
So there is no need of using startActivityForResult and onActivityResult anymore.
In order to use the new API, you need to create an ActivityResultLauncher in your origin Activity, specifying the callback that will be run when the destination Activity finishes and returns the desired data:
private val intentLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.getStringExtra("key1")
result.data?.getStringExtra("key2")
result.data?.getStringExtra("key3")
}
}
and then, launching your intent whenever you need to:
intentLauncher.launch(Intent(this, YourActivity::class.java))
And to return data from the destination Activity, you just have to add an intent with the values to return to the setResult() method:
val data = Intent()
data.putExtra("key1", "value1")
data.putExtra("key2", "value2")
data.putExtra("key3", "value3")
setResult(Activity.RESULT_OK, data)
finish()
For any additional information, please refer to Android Documentation
I have created simple demo class for your better reference.
FirstActivity.java
public class FirstActivity extends AppCompatActivity {
private static final String TAG = FirstActivity.class.getSimpleName();
private static final int REQUEST_CODE = 101;
private Button btnMoveToNextScreen;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMoveToNextScreen = (Button) findViewById(R.id.btnMoveToNext);
btnMoveToNextScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(mIntent, REQUEST_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CODE && data !=null) {
String strMessage = data.getStringExtra("keyName");
Log.i(TAG, "onActivityResult: message >>" + strMessage);
}
}
}
}
And here is SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private static final String TAG = SecondActivity.class.getSimpleName();
private Button btnMoveToPrevious;
private EditText editText;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = (EditText) findViewById(R.id.editText);
btnMoveToPrevious = (Button) findViewById(R.id.btnMoveToPrevious);
btnMoveToPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String message = editText.getEditableText().toString();
Intent mIntent = new Intent();
mIntent.putExtra("keyName", message);
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
In first activity u can send intent using startActivityForResult() and then get result from second activity after it finished using setResult.
MainActivity.class
public class MainActivity extends AppCompatActivity {
private static final int SECOND_ACTIVITY_RESULT_CODE = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// "Go to Second Activity" button click
public void onButtonClick(View view) {
// Start the SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
// send intent for result
startActivityForResult(intent, SECOND_ACTIVITY_RESULT_CODE);
}
// This method is called when the second activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_RESULT_CODE) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra("keyName");
// set text view with string
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(returnString);
}
}
}
}
SecondActivity.class
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
// "Send text back" button click
public void onButtonClick(View view) {
// get the text from the EditText
EditText editText = (EditText) findViewById(R.id.editText);
String stringToPassBack = editText.getText().toString();
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.putExtra("keyName", stringToPassBack);
setResult(RESULT_OK, intent);
finish();
}
}
All these answers are explaining the scenario of your second activity needs to be finish after sending the data.
But in case if you don't want to finish the second activity and want to send the data back in to first then for that you can use BroadCastReceiver.
In Second Activity -
Intent intent = new Intent("data");
intent.putExtra("some_data", true);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
In First Activity-
private BroadcastReceiver tempReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do some action
}
};
Register the receiver in onCreate()-
LocalBroadcastManager.getInstance(this).registerReceiver(tempReceiver,new IntentFilter("data"));
Unregister it in onDestroy()
Another way of achieving the desired result which may be better depending on your situation is to create a listener interface.
By making the parent activity listen to an interface that get triggered by the child activity while passing the required data as a parameter can create a similar set of circumstance
There are some ways of doing this.
1. by using the startActivityForResult() which is very well explained in the above answers.
by creating the static variables in your "Utils" class or any other class of your own. For example i want to pass studentId from ActivityB to ActivityA.First my ActivityA is calling the ActivityB. Then inside ActivityB set the studentId (which is a static field in Utils.class).
Like this Utils.STUDENT_ID="1234"; then while comming back to the ActivityA use the studentId which is stored in Utils.STUDENT_ID.
by creating a getter and setter method in your Application Class.
like this:
public class MyApplication extends Application {
private static MyApplication instance = null;
private String studentId="";
public static MyApplication getInstance() {
return instance;
}
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public void setStudentId(String studentID){
this.studentId=studentID;
}
public String getStudentId(){
return this.studentId;
}
}
so you are done . just set the data inside when u are in ActivityB and after comming back to ActivityA , get the data.
Just a small detail that I think is missing in above answers.
If your child activity can be opened from multiple parent activities then you can check if you need to do setResult or not, based on if your activity was opened by startActivity or startActivityForResult. You can achieve this by using getCallingActivity(). More info here.
Use sharedPreferences and save your data and access it from anywhere in the application
save date like this
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
And recieve data like this
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String savedPref = sharedPreferences.getString(key, "");
mOutputView.setText(savedPref);

Categories

Resources