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
}
}
Related
I am new to android. Currently I am working on android application which uses sqlitedb as the app user may not have continuous internet connection.
My problem is - There is one ReportActivity which shows all data from sqlite in recyclerview.
There is add option which starts AddReport Activity. When I add that new report to server, I start on Service which fetch data from server and store to sqlite. And return to Report Activity.
Now I wanted to update my data in ReportActivity after sqlite db is updated. I tried using interface but it is giving nullpointer exception.
So how I can use interface in that. I don't want to use broadcast receiver.
You can either use startActivityForResult() and return an intent to trigger refresh/fetch the data again on ReportActivity once you finish() the AddReportActivity:
ReportActivity:
public class ReportActivity extends Activity {
private static final int REQUEST_ADD_REPORT = 1;
...
private void showAddReport() {
Intent intent = new Intent(this, AddReportActivity.class);
startActivityForResult(intent, REQUEST_ADD_REPORT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ADD_REPORT &&
resultCode == ACTIVITY_OK) {
// trigger refresh
}
}
}
AddReportActivity:
public class AddReportActivity extends Activity {
...
private void addReport() {
// do some logic on adding the report
setResult(ACTIVITY_OK);
finish();
}
}
Or you can just override onResume() on ReportActivity and move the fetching of data from sqlite in there, so every time the activity resumes, it will always get the updated data.
You can refer to android activity lifecycle on when the onResume() is being called.
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 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.
Is there a way to access to variables on onCreate(), to reuse them in another class?
String testSubCa="";
String prixe="";
String testWiifi="";
Double testPrice=0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_srdata);
testSubCa = getIntent().getStringExtra(SearchRestau.txxtSub);
prixe = getIntent().getStringExtra(SearchRestau.txtprix);
testWiifi = getIntent().getStringExtra(SearchRestau.txxtWifi);
testPrice=Double.parseDouble(prixe);
refresh = (ImageButton) findViewById(R.id.imgRefresh);
refresh.setOnClickListener(this);
}
I need to use these data :/
Since you didn't really specify what exactly your plan is, it's a bit of a challenge getting a proper solution.
I'm just going to assume what you want to do is to start an activity, let the user do something inside this activity and then go back to where it was started from with the ability to use the values created inside the activity.
The easiest way to that is by not starting the activity via startActivity(Intent) but rather by using startActivityForResult(Intent, RequestCode);
In the same activity you then need to handle the event of actually getting a result. To do this you need to implement the method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
//Your code to retrieve the data here!
}
}
The variables should be stored inside the Intent data and can be grabbed by using
data.getStringExtra();
To place the values inside the result you need to place this code in the activity you actually create them:
Intent intent = new Intent();
intent.putExtra("value_id", value);
setResult(RESULT_OK, intent);
finish();
I hope that helps with your problem!
GL
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);
}
}
}