2

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

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

1

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.

Community
  • 1
  • 1
Joe
  • 14,039
  • 2
  • 39
  • 49
  • I get what you are going for, on the situations where I do call finish, and situations where I am not calling finish – CQM Jan 11 '13 at 04:41
  • Yeah, I was under the assumption that you called finish() elsewhere in your code and want to run the special-case code only if that was the case and skip it if your activity is backgrounded due to other reasons (like launching another activity on top, the user presses home, there is an incoming call, etc.). After looking at your answer, it seems that your requirements is a bit different. I will post another update if I got a better idea. – Joe Jan 11 '13 at 04:48
0

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.

CQM
  • 42,592
  • 75
  • 224
  • 366