0

Starting from the Launcher activity i need a way to know what level (hierarchy) the app is at.

For example image the user is in a shopping app & takes the following path:

  1. Main Activity ---> 2. list of shoes to buy ----> 3. shopping cart ---> 4. checkout

so checkout would be level 4. and when the user is just lookign at shoes to buy that would be level 2. I need this for reporting (but thats too much info i think). I just need to know what level/section im at in the app. How can i design something to track this in the activity life cycle ?

update: i have a static variable called level and i extended off Application in my app so i can reset it to 0 each time the activity starts. then in my activities onstart and onstop im adjusting the level but i dont know if this takes care of all scenarios.

  @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        sLevel++;
    }


    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        sLevel--;
}

but its not working. All my activities ( and there are at least 50) extend from an abstract class called baseActivity. in Base Activity is where i'd like to main the level. the end goal is to be able to track where the user came from. I need to report the path the end user took to arrive at the activity

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Um. In onCreate of 2 store a shared pref with value 2? Same for all screens? I'm not getting something. – VM4 Sep 28 '14 at 21:37
  • All my activities ( and there are at least 50) extend from an abstract class called baseActivity. in Base Activity is where i'd like to main the level. – j2emanue Sep 28 '14 at 21:47

3 Answers3

0

I really don't like the way you do that.

Anyway, you should create one activity for each screen as described. Then for each activity you give it corresponding integer value 1-4. Problem solved.

RobGThai
  • 5,937
  • 8
  • 41
  • 59
  • All my activities ( and there are at least 50) extend from an abstract class called baseActivity. in Base Activity is where i'd like to main the level. – j2emanue Sep 28 '14 at 21:47
  • I dont get it, so you have 50 activities then you define fix value in those 50 activities. If you can group them into level, you can create "level" activity then have your activities extending the right level than extending directly from base activity. That's one way to do it. – RobGThai Sep 28 '14 at 21:52
0

Your solution would work if your application navigation is linear. In that case for reporting I don't see why you don't just write out the activity name you're in inside each Activity's onResume() callback.

Keep in mind your solution won't give you desired results if your application workflow is not linear(A to B to C), it it's A has B and C, for example.

HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51
  • the end goal is to be able to track where the user came from. I need to report the path the end user took to arrive at the activity – j2emanue Sep 29 '14 at 01:08
  • I have a navigation bar where user can get to different categories so that might break it being linear. – j2emanue Sep 29 '14 at 01:19
  • based on your end goal, all you need to do is add a log statement to your onResume method in each activity since that callback will be called either when the activity starts for the first time or resumes. – HukeLau_DABA Sep 29 '14 at 02:32
0

Another guy helped me get the path..assuming your using a base class for all your activities. use the code below. Also, if you actually need the level your at as an int then just call getPath() and split the string on the seperator "/". The length of that array would be your level.

public abstract BaseActivity extends Activity {
private static final String EXTRA_PATH = "path";

private String path;

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        path = getPath();
    } else {
        path = savedInstanceState.getString(EXTRA_PATH, "");
    }
}`enter code here`

@Override protected void onSaveInstanceState(Bundle outState) {
     super.onSaveInstanceState(outState);
     outState.putString(EXTRA_PATH, path);
}

public String getPath() {
    if (path == null) {
        path = getIntent().getStringExtra(EXTRA_PATH);
        path = path == null ? "" : path += "/";
        path += this.getClass().getSimpleName();
    }
    return path;
}

    public <T extends Activity> void startActivityWithPathInfo(Class<T> activityClass) {
        Intent i = new Intent(this, activityClass);
        i.putExtra(EXTRA_PATH, getPath());
        startActivity(i);
    }
}
j2emanue
  • 60,549
  • 65
  • 286
  • 456