How to return to a specific action if the actions are performed by one class

To implement up navigation, I would like to return to a specific action in the history stack. If actions on the stack are implemented by different classes, it works like this (assuming I have actions A, B and C on the stack and you want to return to activity A:

protected void onUpPressed() { Intent intent = new Intent(this, A.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); finish(); } 

Android will trigger actions from the stack until the activity specified in the intent is the largest (in this case, the activity performed by class A).

However, my application has several actions on the stack implemented by the same class. This is because they display the same data, but for different objects. They were launched with the intent, which indicated both the class that implements the activity and the object to display (either in the extranet package or in the data property).

Now I am looking for code to pull several actions from the history stack again until the corresponding activity is the largest. If I extend the above code and additionally set the package of additional components or the data property, it does not work. Android always corresponds to the first action implemented by the specified class, and does not return far enough. The component package and data property are ignored.

 protected void onUpPressed() { Intent intent = new Intent(this, A.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setData(Uri.parse("myapp:" + rootId)); startActivity(intent); finish(); } 

So, how can I return to a certain activity? What intent fields can Android compare with to determine if it has found the desired activity?

+7
android android-intent
source share
4 answers

To implement FLAG_ACTIVITY_CLEAR_TOP , Android looks at your task activity stack (top to bottom) using the following test :

  if (r.realActivity.equals(newR.realActivity)) { 

Your problem is that realActivity is a ComponentName (so the comparison above determines the topmost activity on the stack that matches the package and class name ): no further test is performed against the intent, so it’s impossible to be more specific as to which of these the components you want to target.

Thus:

  • So, how can I return to a certain activity?

    There is no equity of its own to achieve this. It is probably best to manually implement the shape of the bubbles as suggested by @ VM4 .

  • What intent fields does Android compare to determine if it has found the activity it wants?

    Only the name of the component.

+2
source share

As you described, you have one action showing different content based on your starting intent. Well, why not Fragments?

I don’t know the architecture details of your applications, but it should be easy to reorganize this activity into a fragment. There may be a FragmentActivity that wraps all the fragments that are responsible for showing the content. Thus, you will have much more freedom to process the stack of fragments actions.

Summary:

  • Convert an existing action (displaying the contents) into a fragment.
  • Make a FragmentActivity (which will manage the fragments).
  • Make FragmentActivity "singleInstance", so it will cache all "startActivity", where you have the option to add a new fragment representing the new content to display.

You can add fragments as follows:

 @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // ContentFragment is the Fragment that you made from your Activity. // Here you can pass the intent that stores the object to show also, // so the parsing of the intent would be the same. ContentFragment fragment = ContentFragment.newInstance(intent); getFragmentManager() .beginTransaction() .add(fragment, null) .addToBackStack("id here that will be used at pop") .commit(); } 

And you can go to a specific id like this:

getFragmentManager (). popBackStack ("id here", 0);

This solution has a side effect. Fragments will stick together, so you cannot insert any other activity between them. This is trivial, but worth mentioning, as it is different from your current implementation.

I also suggested that you are familiar with how SingleInstance and Fragments work. Feel free to ask if something is clear.

+5
source share

I recommend storing your data model outside of additional functions using singleton access to access and updating activity using this data model in onResume ()

0
source share

Suppose you are in /home/usr/vm4/development , and in this exercise you have somekind of Views (say TextViews) that allows you to go to all the parent directories. (Pressing, for example, "usr" will go to /home/usr . In windows, it looks like this:

enter image description here

I do not think that Android will allow you to return to a specific Activity based on additional data. However, you can do the trick here. In a view (which, when you click somewhere), a String tag can be attached to it:

 TextView link = new TextView(); link.setTag("/home/usr"); 

Now when you click this view in the onClick () method:

 onClick(View v) { String extra = v.getTag(); // start your activity with extras here. } 

Now you just need to add the necessary tags to the links when inflating activity.

0
source share

All Articles