How to send data back to MainActivity
You can override onBackPressed
and pass the data back to the original Activity
. But in order to pass it like this, the list must implement Parcelable
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
Bundle data = new Bundle();
data.putParcelableArrayList("someKey", listThatImplementsParcelable);
intent.putExtras(data);
setResult(RESULT_OK, intent);
finish();
}
How do I pass data between Activities in Android application?
The easiest way to do this would be to pass the session id to the signout activity in the Intent
you're using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
Access that intent on next activity:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
How to pass data from 2nd activity to 1st activity when pressed back? - android
Start Activity2 with startActivityForResult
and use setResult
method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult
for updating TextView
with EditText
data from Activity2.
For example:
In Activity1, start Activity2 as:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
In Activity2, use setResult
for sending data back:
Intent intent = new Intent();
intent.putExtra("editTextValue", "value_here")
setResult(RESULT_OK, intent);
finish();
And in Activity1, receive data with onActivityResult
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String strEditText = data.getStringExtra("editTextValue");
}
}
}
If you can, also use SharedPreferences for sharing data between Activities.
Android with Kotlin - Sending data back to MainActivity
You made a mistake:
setResult(0, Intent().putExtra("result", num2))
replace 0 (it cancel result) to Activity.RESULT_OK.
Android:Sending data back to MainActivity
setResult()
does not move you to any other Activities.
Instead of setResult(Activity.RESULT_OK, returnIntent)
you should do this
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);// If an instance of this Activity already exists, then it will be moved to the front. If an instance does NOT exist, a new instance will be created.
startActivity(intent);
And inyour MainActivity onCreate()
you do
Bundle bundle= getIntent().getExtras();
if(bundle !=null){
// get your data here
}
You dont need onActivityResult()
.
The code is in java since i dont know kotlin, sorry :)
Send data back from one activity to another ANDROID KOTLIN
If you open activities like this: A -> B -> C, and want to retrieve result in activity A from activity C you need to do next steps:
- Use some common
REQUEST_CODE
variable, e.g.const val REQUEST_CODE: Int = 400
- In activity A start activity B using method
startActivityForResult(intent, REQUEST_CODE)
; In activity A override
onActivityResult()
method:override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
val message = data!!.getStringExtra("message")
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}In activity B start activity C using method
startActivityForResult(intent, REQUEST_CODE)
;In activity B override
onActivityResult()
method:override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
makeValidations()
// pass data back to activity A
setResult(Activity.RESULT_OK, data)
finish()
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}In activity C to pass data back to activity B:
val intent = Intent().apply {
putExtra("message", "This is the qty $scannedQTY")
}
setResult(Activity.RESULT_OK, intent)
finish()
How do I pass an 'Exercise' object from one activity back to the main Activity
You can use startActivityForResult
instead of startActivity
.
Here is example
class MainActivity : Activity(){
const val REQUEST_CODE = 10001
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// your implementation
Intent(this,SomeActivity::class.java).apply {
// add your data to intent
startActivityForResult(this,REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQUEST_CODE -> handleResult(resultCode,data)
else ->super.onActivityResult(requestCode,resultCode,data)
}
}
fun handleResult(result:Int,data:Intent?){
if(result!=Activity.RESULT_OK) return
if(data == null) return
val someData = data.getSerializableExtra("key")
// do your stuff with someData
}
}
in your SecondActivity
you should use setResult
method to pass your data to caller activity( or MainActivity
in this case).
class SecondActivity : Activity(){
// your implementation of activity
fun sendSuccessResult(someData:Exercise){
setResult(Activity.RESULT_OK,Intent().apply{ putExtra("key",someData) })
finish()
}
}
By calling sendSuccessResult
and passing your data, the data will be send back to your MainActivity
.
You can read more about it here https://developer.android.com/reference/android/app/Activity
how back to the main activity in android?
try this on you onBackPressed:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this clears the activity stack and opens your main activity. No matter which activity you are, you will always go back to the main activity and all other activities are removed from the stack.
Related Topics
Checking If an Android Application Is Running in the Background
Install Application Programmatically on Android
Android Toolbar Center Title and Custom Font
How to Display a Toast from a Background Thread on Android
How to Remove All Debug Logging Calls Before Building the Release Version of an Android App
Android Soft Keyboard Covers Edittext Field
Different Values Folders in Android
How to Use External Jars in an Android Project
Passing Data Between a Fragment and Its Container Activity
Key Hash For Android-Facebook App
How to Manually Include External Aar Package Using Gradle For Android
Determine If the Device Is a Smartphone or Tablet
Android Studio Adds Unwanted Permission After Running Application on Real Device