androidandroid-intentandroid-activityback-stackactivity-manager

How can I go back to the previous activity, without finishing my current activity


Basically, I have an activity which contains the code for a Video conferencing app. And I need to keep that in the background. So that when the user presses a button, I create a FloatingViewService, and send from my activity, the data to that FloatingViewService. And I can for example (This works), start a new intent, and use it to navigate through my app, while my previous activity is still working, and sending the data to the FloatingViewService. Now my problem is that I don't want to start a random activity, but I want to go back to the previous activity, before starting the call. When I start my call, I do it like this:

   Intent intent = new Intent(HomeActivity.this, InCallActivity.class);
    intent.putExtra("url", "url");
    intent.putExtra("test", false);
    startActivity(intent);

And note! After the second activity is created, if I call finish, it will take me to my HomeActivity. Which is awesome. BUT I don't want to finish, I simply just want to go to the activity from which my class is being called.

I tried with: moveTaskToBack(true); but it sends the app to the home screen. Also I made this code:

 ActivityManager m = (ActivityManager) getSystemService(ACTIVITY_SERVICE );
    List<ActivityManager.RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
    Iterator<ActivityManager.RunningTaskInfo> itr = runningTaskInfoList.iterator();
    while(itr.hasNext()){
        ActivityManager.RunningTaskInfo runningTaskInfo = (ActivityManager.RunningTaskInfo)itr.next();
        int id = runningTaskInfo.id;
        CharSequence desc= runningTaskInfo.description;
        int numOfActivities = runningTaskInfo.numActivities;
        String topActivity = runningTaskInfo.topActivity.getShortClassName();
        Log.i("","top activity: " + topActivity);
    }

Thinking that I can reinstate the activity from the backstack, using the FLAG_ACTIVITY_REORDER_TO_FRONT

BUT, I don't get my activity back, why is that? All I get is:

12-06 13:17:54.213: D/HeartRateApplication(3168): top activity: com.vidyo.vidyocore.activities.WebViewActivity
12-06 13:17:54.213: D/HeartRateApplication(3168): top activity: com.google.android.launcher.GEL

But if I do finish, clearly my HomeActivity is there, because it takes me back to it. Why don't I get it in my list from the running activities? And how can I fix this?

EDIT:

There was a comment that said that I should start the activity with a intent. So I feel maybe I was not clear enough. I cannot start an intent to a default Activity. There is a chance that this InCallActivity will be called from multiple activities. Hence I need to know what activity was before it, so I can call an intent to that activity, WITH like I said FLAG_ACTIVITY_REORDER_TO_FRONT Hence the logic to use the runningTaskInfo but that doesn't not provide me with the whole backstack. Is there another way to get the backstack, so I can actually know whay activity to start?


Solution

  • Start the activity using Intent.FLAG_ACTIVITY_CLEAR_TOP

    Also you can refer this

    String Activity name= getIntent().getStringExtra("mActivityName");
    

    use this to track your activity flow and name.