0

Couldn't find a good answer for this. I have a class(no opened from MainActivity. There I want to call startActivityForResult, to know when to do something on the UI. How do I do it correctly? I passed the activity and context to the class. On class:

private void init(){
        Intent TestIntent = new Intent();
        mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
    }

On MainActivity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
        super.onActivityResult(requestCode, resultCode, Data);
        if (requestCode == TEST){
            Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
        }
    }
Omri
  • 95
  • 5
  • Does this answer your question? [How to use StartActivityForResult()](https://stackoverflow.com/questions/37768604/how-to-use-startactivityforresult) – isaaaaame Jan 28 '20 at 14:01
  • Why do you want the class to call startActivity? Passing in an activity to a class is not the best thing as the activity might be destroyed and your class will still hold a reference to it. – tomerpacific Jan 28 '20 at 14:02
  • Did you call `setResult()` when finishing the second Activity? – Ivan Wooll Jan 28 '20 at 14:03
  • I just want to activate something on the main activity from an event inside this class. Like a listner from a class. Ivan, the there is only one activity here. – Omri Jan 28 '20 at 14:59

3 Answers3

0

Check this, You have to pass code with Intent of Required Class

private void init(){
    Intent TestIntent = new Intent(this,SecondClass);
    startActivityForResult(TestIntent,222);
}



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
        super.onActivityResult(requestCode, resultCode, Data);
        if (requestCode == 222){
            Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
        }
    }
Chandan kushwaha
  • 941
  • 6
  • 26
  • The function that calls startActivityForResult isn't inside an activity, and I can't compile while changing what you suggested: Intent TestIntent = new Intent(this,MainActivity.class); Says 'Cannot resolve constructor...' I did use a pass code(it is defined in MainActivity) : public final static int TEST = 3; – Omri Jan 28 '20 at 15:01
  • If it is not in an activity then you have to get the reference of that activity and use that for calling startActivityForResult. However placing it in Activity would be a good suggestion. – Chandan kushwaha Jan 29 '20 at 04:08
0

Your TestIntent is empty. It doesn't have anything in it that tells Android what Activity to start. You need to create your Intent like this:

private void init(){
    Intent TestIntent = new Intent(mActivity, ActivityToStart.class);
    mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thank you for the comment. if I do this, it goes into a loop of starting and turning off the acivity all the time or something like that. I guess it's related to the fact that mActivity is the same activity I want to start - meaning the class(of init function) is activated from MainActivity. Nevertheless, OnActivityResult does not respond. – Omri Jan 28 '20 at 23:34
  • Why are you trying to start an `Activity` from itself? – David Wasser Jan 29 '20 at 07:58
  • I read in a comment above that you just want to call a method on that `Activity`. Since you have a reference to it, you don't need to call `startActivity()`, you can just call the method directly using `mActivity.whatEverMethod()`. Sounds like you are confused about what `startActivityForResult()` is. – David Wasser Jan 29 '20 at 08:01
  • David Wasser, you are probably right. now I tried to call functions from MainActivity just as you suggested, but it doesn't recognize them(despite them being public). any thought? – Omri Jan 30 '20 at 14:01
  • 1
    ok, the problem was that I took a reference to android app activity not MainActivity. – Omri Jan 30 '20 at 14:51
  • Yes, I passed the context from main activity and used it to simply call functions from it, as I wanted. – Omri Feb 09 '20 at 12:15
  • Great that you have solved your problem. Please accept one of the answers, or write your own answer and accept that. That will remove the question from the list of unanswered questions. – David Wasser Feb 09 '20 at 23:08
0
private void init(){
Intent intent = new Intent(this,another.class);
startActivityForResult(intent,requestCode);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
    super.onActivityResult(requestCode, resultCode, Data);
    if (requestCode == reruestCode){
        Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
    }
}


  private void getResponse(){
            setResult(RESULT_OK,new Intent());
          finish();
//Use this block of code in the second class.
        }
Anand Pandey
  • 562
  • 5
  • 6