Question

I think I need to put some code within my onStop method. It pertains to a service that should be running only when the activity is finished()

but when the user follows some linkify'd text to the web browser, or when the user presses the homescreen, both call onStop() but these do not end the activity, I don't want to end the activity when a user follows a link, so I can't put finish() within onStop() unless I can detect and differentiate when this happens within onStop()

is there a way I can override Linkify() so that I can add a flag within it, or maybe make it run startActivityforResult() so that I can information back in a result?

similarly, is there a way I can set the activity to finish() when the user presses the home button?

Thanks

Was it helpful?

Solution

Is it possible for you to check isFinishing() in your onStop() to decide whether you need to run the service-related code or not?

@Override
protected void onStop() {
    super.onStop();

    if (isFinishing()) {
        // Your service-related code here that should only run if finish() 
        // was called.
    }
}

UPDATE: (after understanding the problem better)

Very similar to my suggested approach on another question, you can probably override startActivity() to intercept when the link is launching and set your flag if that's the case.

@Override
public void startActivity(Intent intent) {
    final String action = intent.getAction();
    if (action.equals(Intent.ACTION_VIEW)) { 
        // maybe also check if getScheme() is 'http' then set our flag
        persist.saveToPrefs("linkifyClick", true);
    }

    // proceed with normal handling by the framework
    super.startActivity(intent);
}

That other answer also show how you can call startActivityForResult() too instead if you want.

OTHER TIPS

SOLUTION (with still a remaining problem)

I put an onClick: attribute on my textView in the xml

<TextView
        android:id="@+id/body"
        android:paddingTop="10dp"
        android:onClick="onTextViewClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

and then saved a flag within my sharedpreferences within that new method

public void onTextViewClick(View v){

    //write to sharedpreferences
    persist.saveToPrefs("linkifyClick", true);
}

and then in my onStop I can detect whether that flag is set or not!

 @Override
public void onStop(){
    super.onStop();



    if(persist.getFromPrefs("linkifyClick", false) == false)
    {
        finish();
    }
    else
        persist.saveToPrefs("linkifyClick", false); //if it was true, then set to false
}

PROBLEM

It is possible to click on the linkified text without touching the actual link portion. This sets the flag to true and still mixes up the lifecycle I am going for. So I need to be able to detect when the home button is pressed

UPDATE: this helped clarify the problem for the other poster (with the accepted answer), that person updated the answer and I accepted it after I put it in my code. It works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top