简体   繁体   中英

Is creating a new Activity on pressing a home button a sloppy solution?

So I have a home button on my App Bar that I implemented which can be clicked from pretty much anywhere in the App, and the exact code in the listener for menu items is

if (id == R.id.button_home) {
        startActivity(new Intent(this, MainActivity.class));
        return true;
    }

As the code would suggest, this just creates a new activity taking the user to the home page however I have two concerns.
1. The potential loss of information, since I currently do not pass any data and it is a new activity.
2. I don't believe that I'm actually destroying any of the previous activities. Could this lead to decreased performance and slow down the application, as you'd end up with redundant activities being retained?

To answer your questions

  1. If you don't pass any information, the data will not persist in whatever state you have it if you were to call a new Intent over to that activity. We can't know exactly what will be lost though, without knowing what you have in there. The safest bet would be to store that data locally in a sqlite3 db, and call it up every time the activity loads, or if it's not that much information, load it up in a shared preference.

  2. Like Time says, you don't create a new activity. You have already done that in the manifest. What you're doing with an Intent is calling the activity that has already been created.

Depends on what do you want to do. If you changed some configuration it's ok to do that with adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP to your Intent instance.

Or, if you just want to remove Activity from the stack, just call onBackPressed() .

How about launching the activity with

Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

more about it here

It brings your activity to the top of the activity stack if it already exists or creates a new one if it doesn't.

You shouldn't retain Activities in the first place. Androide lifecycle methods will take care oft handling Apps ging to background. As long as you don't cause Context leaks, you should be find.

The next thing to keep in mind is the fact that you don't create a new instance with your Intent call. The system decides what to do ( hence " intent").This may include jumping back to an existing instance.

Have a look at the Intent flags ( http://developer.android.com/reference/android/content/Intent.html ) and choose your desired behavior ( think about back button as well) So yes: this is perfectly legal if it is the behavior you want

  1. You can use sharedpreference to store the data or you can pass the data with intent.

  2. You can clear the activity stack:

     Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM