-5

I am trying to create a new instance of an activity from within the same class of the activity. For instance I have a Activity called Settings.

Intent intent = new Intent(Settings.this, Settings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("ShowBookmarks", true);
startActivity(intent);

And in my AndroidManifest I don't have any launch options set, though I have tried with singleTop and a couple others.

The goal is to display different data in the same controls (Recycler view, etc), and then be able to click back to show the previous data.

linuxer
  • 523
  • 2
  • 4
  • 22
  • Sounds like a xy-problem.... re-think your approach – B001ᛦ May 24 '18 at 12:04
  • @B001ᛦ A little guidance would be helpful. The only other solution I have been able to come up with doesn't give the user flow that I want. That is just to reload the data, but then it won't provide the back icon without having to add it manually, and I would prefer to not do that. – linuxer May 24 '18 at 12:07
  • _A little guidance would be helpful..._ I think @Rohit5k2 said all – B001ᛦ May 24 '18 at 12:07
  • I don't know if this will work. Sounds dangerous (but hey, living on the edge, right?). Why don't you use various instances of a fragment? Or even better, different fragments? If your goal is just to display data, fragments sound good. – Luís Henriques May 24 '18 at 12:08
  • 1
    Bad design. Change data not activity. Take Fragment approach. – Rohit5k2 May 24 '18 at 12:08
  • My problem is I inherited this project from a very poor developer and I don't have the time to write things in a way to use fragments. I'll see what I can do. Thanks – linuxer May 24 '18 at 12:09
  • Another approach would be to update the views in your activity, and keep the data in arrays or dictionaries (Maps, in Java). Then simply iterate through them as you require. – Luís Henriques May 24 '18 at 12:10
  • Why don't you try using [Fragments](https://developer.android.com/guide/components/fragments) instead? You can learn more about them [here](https://www.tutorialspoint.com/android/android_fragments.htm). – Joey Dalu May 24 '18 at 12:10
  • @LuísHenriques That was the other option I had considered, and I think that is the best option I have in my current timeline/situation. – linuxer May 24 '18 at 12:11
  • Yeah, I think that is your best option as well, given the circunstances. Good luck. – Luís Henriques May 24 '18 at 12:12
  • 3
    Please stop voting down this question. Just because you don't agree with the approach, doesn't mean it doesn't belong in Stack Overflow. After all, we are trying to build a hub of knowledge for future use as well. It is good to register that this is not the best approach, and which are the alternatives. – Luís Henriques May 24 '18 at 13:10

2 Answers2

1

Intent intent = new Intent(Settings.this, Settings.class);

I don't know if that's possible | Re-think your strategy

But, if you want to just restart your activity:

You don't have to write those long things.

Just call onResume(); from anywhere.

I do that in this way.

Also,

Since API level 11, you can call the recreate(); method of the Activity.

exploitr
  • 843
  • 1
  • 14
  • 27
0
  • Create two Fragments or you can manage using one fragment means go with one fragment.

  • Add the fragment into backstack (addToBackStack(String name)) with different name.

  • Pop the fragment in onBackPressed() method.

It will work as you expected.

Note: All your activity related code move into Fragment.

Santhosh
  • 160
  • 7