Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

java - Null listener when called from another activity

In Android, I am trying to setup a callback listener for when a task is complete in a second activity. Flow is as follows:

MainActivity Starts -> Calls Second Activity -> When second activity completes call listener - > Return to Main Activity

I have done this with the following code:

  //// First activity
  Main Activity {


      // Reference to second activity
  private IntroActivity mIntroActivity;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    /// Listener reference
    mIntroActivity = new IntroActivity();

    Intent i = new Intent(MainActivity.this, IntroActivity.class);
  startActivity(i);


      /// Call back for second activity
    mIntroActivity.setIntroListener(new IntroActivity.IntroListener() {
        @Override
        public void finished() {
          /// When finished do something!
        }

    });

  }
  }


  /// Second Activity

  public class IntroActivity extends AppIntro {

      /// Listener setup

    public interface IntroListener {
        void finished();
    }


    public void setIntroListener(IntroListener listener) {
        this.mIntroListen = listener;
    }

    public IntroActivity(){
        this.mIntroListen = null;
    }


    private IntroListener mIntroListen;



      @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

    }

    @Override
  public void onDonePressed(Fragment currentFragment) {
      super.onDonePressed(currentFragment);

      /// Call listenr from MainActivity
      mIntroListen.finished();
  }



  }

Adding an interface listener to the second activity. When this activity is complete call finished() and return to MainActivity.

The issue I'm getting is mIntroListen.finished(); is null in the second activity.

Any ideas here would be great. I have the same interface / listener working in another project (which is another class not activity) so I'm not sure whats wrong.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue I'm getting is mIntroListen.finished(); is null in the second activity.

You will always get null because mIntroActivity instance and your actual running instance of IntroActivity are different.

mIntroActivity = new IntroActivity();

Creating activity this way is not a good idea. To start a Activity you should always use startActivity().

If you want to get back to MainActivity with some data from IntroActivity or just back when completes call listener you should use startActivityForResult(intent) in MainActivity. This method will give you options to set result you have found in IntroActivity and you can use those result in MainActivity by onActivityResult() method.

For your case use startActivityForResult(intent) to start your IntroActivity and when completes call listener just finish IntroActivity then onActivityResult() method will be called in MainActivity. And do what you want in onActivityResult()

Check this official link Getting a Result from an Activity and you can also check this answer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...