Android Getting Results from another activity - java

I have a problem with my code. I want to pass a String from the SecondActivity to FirstActvity. Note that the FirstActivity is not visible but its still open. when the SecondActivity is finish it passes a String to the FirstActivity.
My problem here is that when the SecondActivity ended and goes to FirstActivity, the whole application closes.
FirstActivity to SecondActivity:
Intent intent = new Intent(MainActivity.this, FileChooser.class);
startActivityForResult(intent, 0);
SecondActivity to FirstActivity:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("filePath", "/sdcard/path1");
setResult(0);
finish();
FirstActivity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
Intent intent = getIntent();
this.filePath = intent.getExtras().getString("filePath");
}
What is wrong with the code?

When you set the result of your SecondActivity, you only set the result code. Instead of setResult(0) use setResult(0,intent)
Also, in your FirstActivity's onActivityResult get the extra from the data argument - this.filePath = data.getExtras().getString("filePath");

Try to use
data.getExtras().getString("filePath");
instead of
intent.getExtras().getString("filePath");`

Try with Bundle:
First Activity;
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("filePath","/sdcard/path1");
intent.putExtras(bundle);
startActivity(intent);
}
Second Activity:
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
value = extras.getString("filePath");
}
}

try this
example.It solves your problem.

Related

Refresh Activity

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

How to send data from the 5th activity to the 1st activity?

I am using 5 activities. from activity1 I move to activity2, from act2 to act3, from act3 to act4 and from act4 to act5.
activity 2 carries data to act3 and like this act5 receives data of act2, act3, act4 and then send all data to act 1.
My First activity
I{
Intent i= new Intent(firstactivity.this, secondactivity.class);
startActivityForResult(i, 10);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
String a= data.getStringExtra("Value1");
String b= data.getStringExtra("Value2");
String c= data.getStringExtra("Value3");
String d= data.getStringExtra("Value4");
String showall = a+", "+b+", "+c+", "+d;
address.setText(showall);
}
My Second activity
Intent intent = new Intent(secondactivity.this, thirdactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra("Value1", firstvalue);
startActivity(intent);
My Fifth activity
Intent intent = new Intent(fourthactivity.this, fifthactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
setResult(10);
finish();
Try below:
Intent intent = new Intent(fifthactivity.this, firstactivity.class);
intent.putExtra("Value1", geta);
intent.putExtra("Value2", getb);
intent.putExtra("Value3", getc);
intent.putExtra("Value4", getd);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
And then process your passed result in onCreate (in case activity got destroyed) or onNewIntent (if your activity is still running but you bring it to front and update it with new intent) of your firstactivity
//A simple approach to solve this issue is to use sharedpreferences
//store value from activity five
getsharedpreferences('temp','MODE_PRIVATE').edit().clear().putString('values','new value').apply();
//get values from shared preferences if its available
String value 5 = getsharedpreferences('temp','MODE_PRIVATE').getString('values','nil');
getsharedpreferences('temp','MODE_PRIVATE').edit().clear();
first activity
static ArrayList<String> arlist=new ArrayList<String>();
arlist.add("value");
arlist.add("value1");
arlist.add("value2");
In second Activity
ActivityOne.arlist

Move data by intent without startActivity

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

Call method when intent closes

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

how to handle a getIntent() method with java nullpointerexception

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

Categories

Resources