How to get getIntent() to return null after Activity called with an Intent set

10,241

I found a solution that appears to work, but I expect there is a better way to do it.

    if (getIntent() != null && getIntent().getData() != null) {
        String data = getIntent().getDataString();
        getIntent().setData(null);
        setIntent(null);

I think the setIntent isn't of any use, but the setData(null) seems to do the trick.

Share:
10,241

Related videos on Youtube

James Black
Author by

James Black

I have been programming since I was in high school, and my favorite computer was my Amiga 2000, as that is what I learned to program in C on, and was just a great computer. I have been an advocate about unit testing since 1999, and believe we can get better code by getting before the user more frequently so we can narrow the solution to what they really want, as users don't really know what they want, initially. I am comfortable with many languages, but am trying to understand how to program in functional languages, giving up while loops is very difficult for me in designing applications. :)

Updated on June 04, 2022

Comments

  • James Black
    James Black almost 2 years

    This question was similar to my original question, but I think there is a better way to approach a solution.

    getIntent returns wrong intent when setIntent is followed by a rotation

    Basically, in my main Activity, which extends FragmentActivity, there are two instances where in a Fragment I pass an Intent to this Activity.

    This is the code that is having the problem:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
    
        if (getIntent() != null && getIntent().getData() != null) {
            String data = getIntent().getDataString();
            //setIntent(null);
    

    The setIntent was an attempt to solve my problem but it failed to work.

    What happens is when I click on a link that goes back to my Activity as an Intent, then I go to the next Fragment, then I rotate the phone, the Intent that was passed two Fragments ago shows up again when I call getIntent.

    So, when I get the data out of the Intent, how can I set it so the next call to getIntent will return null? Or, would it make sense to just have the data in the Intent be null instead?

    I am trying to avoid updating information in the SharedPreferences to handle this.

  • M. Reza Nasirloo
    M. Reza Nasirloo about 6 years
    Does not work on API 27, Haven't tested on other APIs.