I am calling Settings activity from another activity using the startActivityForResult method. When the back button is clicked, it goes back to the screen on my application. But this activity had already been loaded before calling the settings activity, so I want to be able to refresh the activity on back click on the settings activity. How do I do that?
You will want to implement the onActivityResult() method in your first activity. This method will be called any time an activity you've started with startActivityForResult() finishes.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == yourRequestCode) {
// your code here
}
}
In the case of the BACK button, the resultCode argument will be Activity.RESULT_CANCELED. This doesn't really change things, but lots of examples will include checking for resultCode == Activity.RESULT_OK and I just wanted to mention that it's perfectly fine to do things even when the result code is something else.
Just use
#Override
public void onResume(){
super.onResume();
// your code...
}
In the method implement what has to be needed to be updated.
Related
For example I am starting a activity which finishes and returns a result, which is returned by the method onActivityResult. How can I wait before the result is provided by the onActivityResult and then continue my reactive stream?
public class TestActivity extends AppCompatActivity {
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// receiving result here
}
private Single<Data> loadData() {
return Remote.getInstance().getData()
.flatMap(data -> {
// do something
Intent intent = new Intent(TestActivity.this, OtherActivity.class);
startActivityForResult(intent, 0);
}) // how can I receive the result from the onActivityResult here?
}
}
It would be possible for example by using a PublishSubject that would notify in onActivityResult() and to which you'd flatMap() to in your original call, but I'd strongly advice against this. Here is why:
When your Activity receives an onActivityResult(), it might have been just recreated from scratch, because of a configuration change, memory pressure or anything else that happened in the meantime. This means that your Single subscription is either no longer active (if you properly unsubscribed from it onDestroy() or better in onStop()) or leaked the previous activity instance to what it is still attached to (in case you did not unsubscribe).
So, instead the stream (and the aforementioned PublishSubject) should probably live inside a component that survives activity recreation, like a retained fragment and you should call into that in onActivityResult() to notify your stream about the results. Here is some example how retained fragments and RxJava can work together.
I use an Intent in MainActivity to start another Activity called MatchesActivity using startActivityForResult. I put breakpoints at finish() inside the started activity (MatchesActivity) and on the Log statement inside onActivityResult because I got NPE inside onActivityResult.
intent.getStringExtra("TXAMATCHES") contains what it should.
But Intent data comes back null.
Is this likely/possibly because I start the Intent inside an onClick listener?
Or because MatchesActivity calls a method in another class that extends AsyncTask and the data is what that task produces? (But the correct data is found in txaMatches just before it is returned to MainActivity...)
public class MainActivity extends Activity
{
...
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v)
{
...
Intent matchesIntent;
matchesIntent = new Intent(MainActivity.this, MatchesActivity.class);
startActivityForResult(matchesIntent, 0);
...
}
...
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.w("MainActivity","" + onActivityResult " + data.getStringExtra("TXAMATCHES"));
}
}
...
public class MatchesActivity extends Activity implements DatabaseConnector.DatabaseProcessListener
{
...
#Override protected void onDestroy()
{
super.onDestroy();
finishThisAndReturnString();
}
public void finishThisAndReturnString()
{
Intent intent = new Intent();
intent.putExtra("TXAMATCHES",txaMatches.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
(The started Activity is terminated by user pressing back icon.)
You can't finish a activity in the onDestroy method. In this lifecycle phase the activity is already dying.
The call of the finish() method will call the onDestroy, not the other.
Call the finish method when you want to to finish the MatchesActivity.
What you're trying to do is not correct. You're trying to call finish() on the onDestroy() method. onDestroy() gets called when the Activity is about to be killed. So basically calling finish() at that point is wrong.
I assume that you are trying to do some processing in the AsyncTask. I suggest that you call the finish() method on the onPostExecute() method of the AsyncTask.
You are setting the data in the Intent object in onDestroy() method . So in this case, whenever you call setResult() in onDestroy() method, you will always get resultCode in onActivityResult() as '0' which is the value for RESULT_CANCELED and whenever you get RESULT_CANCELED in onActivityResult() you get data object null. That's why you don't get the values you set in Intent .
First try to get the value of
txaMatches.getText().toString()
Check this value in the MatchesActivity Class.
It is possible that it is returning null value due to which u are getting the problem.
The problem is fixed by removing finishThisAndReturnString() from onDestroy, as stated in the first two Answers.
I am quite new to Java and would appreciate if someone could explain show me
how can I implement startActivityForResult(Intent, int) and
onActivityResult() in Bluetooth discoverability stated
here.
What I want to achieve is: on button Enable BTDisco click
(if(bttn.getId() == R.id.bt_disco) my program calls
BTDiscoverable() method:
public void BTDiscoverable() {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
BT_ENABLE_TIME);
startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
}
Now, the dialog pops up. If user clicks no, the program should not
continue. If user click yes, Enable BTDisco button should become
unavailable and another button, lets call it Start, becomes available. I
wrote onActivityResult which would make Start button available but I
doubt it is done right. Snippet here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
if (requestCode == REQUEST_ENABLE_BT)
{
if(resultCode == RESULT_OK)
{
((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
BTCountDown = new BTTimer(BT_TIME_BTTIME,BT_TIME_BTTIME);
BTCountDown.start();
}
}
}
startActivityForResult is called in BTDiscoverable method but it does
not provide what I wanted. I have followed this but as I am quite
new here, I have no idea how to implement this in such problem as mine.
The onActivityResult method is implemented class that extends Activity.
There are no errors while compiling. Long story, short: R.id.bt_server_start stays disabled as initially programmed.
Could really use some help.
Just put this
if(resultCode != RESULT_CANCELED)
Documentation says the following
Your activity will then receive a call to the onActivityResult()) callback, with the result code equal to the duration that the device is discoverable. If the user responded "No" or if an error occurred, the result code will be RESULT_CANCELED.
So if result code is not RESULT_CANCELED, then you are okay to enable your button.
I need to have gridview scrolling to the first item onResume of my activity. It has only the vertical scrolling.
I have tried the following, but it didn't work...
gridView.setSelection(0);
gridView.scrollY(0);
Can someone help please...
I would try gridView.scrollTo(0,0) or gridVIew.smoothScrollTo(0,0), depending on your preference.
Edit: Updated with fixed code.
I have solved my problem!. I have implemented onActivityResult and called the activity for adding a new item, with a known request code. Then upon return, and the result was OK; I use gridView.smoothScrollTo(0), which worked like charm! See code below...
Intent intent = new Intent(this, AddNewActivity.class);
startActivityForResult(intent, REQUEST_ADD_NEW);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_ADD_NEW_CLOTHING_ITEM)
{
gridView.smoothScrollToPosition(0);
}
}
}
I'm working on an app which uses default Camera app for taking a photo. Now I want to check is there were taken the photo.
I read on SO that that could be done with this method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
}
My problem is when I put that method in my class which isnt an Activity class I got error:
"The method onActivityResult(..) must override or implement a supertype method"
I already check, my compiler is set to java 1.6. and my jre is set to 1.6.
What should I do?
You need an Activity on order to receive the result of the camera action. Why do you want to put the code in some other class? If it is just for re-use/organisation then why not just call your other class from your Activity?
public class SomeOtherClass {
public static void someOtherMethod(int requestCode, int resultCode, Intent data){
...
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SomeOtherClass.someOtherMethod(requestCode,resultCode,data);
...
}
You can only get the result from an Activity inside another Activity or Fragment.
Therefore you can only extend onActivityResult() from a class that extends Activity or one of it's descendants (This is actually java-related and not android-related).
Also, you must extend and define onActivityResult() in the same activity where you call startActivityForResult().
If you need to execute code in another class that does not extend Activity, you can simply implement your own method and call it from inside onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == MY_REQUEST_CODE && resultCode == RESULT_OK)
{
MyClass object = new MyClass(); //or maybe it's already instantiated and it is defined as a field
object.myCustomMethod(data); //pass it data or one of it's extras
}
}