This is my scenario
Activity A (List of user)
Activity B (User Add Activity)
Desired Flow in app
Step 1: A ==> B
Step 2: B ==> A
Step 3 (Required flow):
Activity A has to reload itself or call loadListView() function after Activity B exits or is closed.
onCreate(){
.......
....
btnAddClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
loadListView();
......
....
}
Try to call loadListView() in onStart or onResume methods. It will refresh the list when you come back to your activity
Getting a result from an activity
From your Activity A, call the Activity B using the startActivityForResult() method.
int RESULT_CODE = 123;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, RESULT_CODE);
Now in your ActivityA class, write the following code for the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_CODE) {
if(resultCode == Activity.RESULT_OK){
bool status=data.getBooleanExtra("isUserAdded");
if(status)
{
loadListView();
}
}
}
} //onActivityResult
Return back to ActivityA with status.
Intent returnIntent = new Intent();
returnIntent.putExtra("isUserAdded",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Related
I Have an activity A that opens an activity B using startActivityForResult.
Now in activity B it's an activity fragment holder as well it contains an ActionBar with menu items.
Now whenever I press action bar button in activity B it should return data from selected fragment of an activity B not to its holder instead it should return data to activity A because it's the one who did the launch.
So it's basically passing data fragment (inside activity B) to activity B then to Activity A.
I am trying hopelessly to find a way to solve it. Is there any possible way to do it?
Disclaimer there are many ways, this is the one I prefer, not the best ever and not the perfect one, I just like this.
The easiest way, in my opinion, is to pass the data from Fragment inside B to ActivityB, then from ActivityB to ActivityA.
Step 1 to pass data from Fragment to container activity you have many ways; the one I usually use is to use an Interface:
Create interface for ActivityB
public interface IActivityB {
void setDataAAndFinish(whateverType data);
}
Implement interface in your activityB
public class InterventoActivity extends AppCompatActivity implements IInterventoActivity {
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private Bundle dataA = null;
#Override
public void setDataAAndFinish(whateverType data) {
dataA = data;
Intent intent = new Intent();
intent.putExtra("data", data)
setResult(RESULT_OK, intent);
finish();
}
}
Set activityA to request and accept return from ActivityB
first, start activityB for result and not normally
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
Then read result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
whateverType data = data.getStringExtra("data");
}
}
}
Now from fragment
((IActivityB)getActivity()).setDataAAndFinish(myDatas);
You need to declare a function in your ActivityB like the following.
public void sendDataBackToActivityA(String dataToBePassedToActivityA) {
Intent intent = new Intent();
intent.putExtra("data", dataToBePassedToActivityA);
setResult(RESULT_OK, intent);
finish();
}
Now from the Fragment that you launched from ActivityB, just call the method on some action in your Fragment that was launched from ActivityB. So the pseudo implementation of the process in your Fragment should look like the following. Declare this function in your Fragment and invoke the function on some action in your Fragment.
public void sendDataToActivityAFromFragment(String dataToBePassed) {
((ActivityB)getActivity()).sendDataBackToActivityA(dataToBePassed);
}
This will serve your purpose I hope.
You can declare static variables in your A activity and set it from the B activity
Or you can make static class and set variables values from B Activity and get it in A activity
Or you can send the menu value from Activity B to A while you exit Activity B and start activity A by using bundle
this is how you set the data to be passed to activity A in activity B
val resultIntent = Intent()
resultIntent.putExtra(DATA, "closed")
setResult(Activity.RESULT_OK, resultIntent)
finish()
this is how you get data in activity A
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == FILE_UPLOAD_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
// data here is obtained data of the method paramater
}}}}
In your Activity A.Move from activity A to b like this:-
int YOUR_CODE=101; // it can be whatever you like
Intent i = new Intent(getActivity(), B.class);
startActivityForResult(i,YOUR_CODE);
In your Activity B in its fragment
Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();
And in your Activity A's onActivityResult() function do this:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (resultCode==YOUR_CODE)
{
String value = (String) data.getExtras().getString("NAME OF THE PARAMETER"); //get Data here
}
}
}
In kotlin: -
In class A
startActivityForResult(Intent(context, B::class.java), 143)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 143) {
if (data!!.extras.get("check").equals("0")) {
childpager.currentItem = 0
}
}
}
In Class B
val intent = Intent()
intent.putExtra("check", "0")
setResult(Activity.RESULT_OK, intent)
I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture
I have four activities:
Activity A
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
}
Activity B
Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
intent.putExtra(EXTRA_MESSAGE, uunitValue);
startActivityForResult(intent, REQUEST_CODE);
Then in the second activity I have to pass data to the third activity.
Activity C
Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
intent.putExtra("card", card);
startActivity(intent);
finish();
Activity D
Intent data = new Intent(Stage3Activity.this,MainActivity.class);
data.putExtra("data", card);
startActivityForResult(data, RESULT_OK);
When I press the button on Activity A, the Toast is not shown.
You need to update your code as follow
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
First check for RESULT_OK and then proceed further
Happy Coding!
Replace getApplication() with this (the context of the current activity)
Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
Use getApplicationContext() instead of getApplication() in your makeText() method
I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
I have 2 activities in my application, Activity1 and Activity2.When the app launches Acitivity1 is the the one that is called.Clicking on a button in Activity1 should take you to Activity2.
In Acivity2,some data processing is done then i send back data to Activity1 using an intent like this:
Intent in=new Intent(getApplicationContext(), Activity1.class);
in.putExtra("data", data);
startActivity(in);
Then getting back to Activity1 i obtain the intent data:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
String data =(getIntent().getExtras().getString("data"));
The problem here is that the first time the app launches it checks for the intent data and it does not exist so i get the nullpointerexception error.how can i make sure it checks for the intent data when Activity2 is the previous class?
You can check if the intent is existing using:
getIntent().hasExtra("data");
This will return you a boolean.
Also if oyu want to return some datas to the first activity, your should start the second one with startActivityForResult
if (extras != null) {
if (extras.containsKey("data")) {
boolean hasData = extras.getBoolean("data", false);
// TODO: Do something with the value of "data".
}
}
put a check over it
if(getIntent().getExtras()!=null){
String data =(getIntent().getExtras().getString("data"));
}
Follow the following steps
1: from your activity1's button click call as follow
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
2: In your Activity2 set the data which you want to return back to Activity1 as follow and if you don't want to return back don't set anything.
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
if you don't want to return data:
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
3: now again in your Activity1 handle the return data as follow
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
EDIT:
and if you strictly want to use your own method as in your question try this:
in your Activity1 you obtain the intent data as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
String data ="default value";
try{
data =(getIntent().getExtras().getString("data"));
}catch(Exception e){
}
}
use ActvityForResult.
replace these codes.
in ACTVITY1:
put this after onCreate:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name =data.getStringExtra("data");
if(resultCode == RESULT_OK){
switch(requestCode){
case 2:
if(resultCode == RESULT_OK){
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
}
}
in ACTIVITY2:
Intent intent=new Intent();
intent.putExtra("data",data);
setResult(2,intent);
finish();