Start a new activity or setContentView? - android

Is it better to start a new activity or just set a new content view in Android?
I usually start a new activity when I have to change the whole environment, but IMHO I think it's too onerous when I need to keep the other activity alive in the background.
On the other hand, I could have a single activity and just change the layout when I have to set a new environment.
Which is the better way and why?

Search for 'Pattern one activity, multiple views. Advantages and disadvantages' on stack-overflow and look for the answer by #CommonsWare

Related

What's the best way to create items programmatically inside a Service?

So, I have to create few items, like Buttons and TextViews, from a service.
Since I need to pass along a Context when I create a new one, and since my service doesn't have one, I thought about doing it like this
Button button = new Button(getApplicationContext());
It works, but is it the best solution? I read a lot about how you have to be extra careful when using application context, so I'm not sure if it's the proper way to achieve what I need
Thanks in advance
I never used Service instances for that so, this is new for me. However, I stumbled upon this question about overlay service which gives quite cool solutions. As per your question I guess the following should work within the scope of your Service and is safer:
Button myButton = new Button(this);

How to call a new activity without calling a new "window"

I am trying to call a new activity using an intent, but every time I call it, I can see a new "window" open on my android device. Can I call a new activity that will be in the same window? What I mean is calling new activity without visually seeing that it has been opened.
Hope you understand my question :) Thank you!
I think what you are looking for is Fragments
Start reading Here
Fragments actually use the "same" window (Activity) and just replaces layouts and views - I think exactly like you want.
If you want to run something like a unix-demon code(this mean a program that is only executed in the background without no visual components) you are looking for a android Service.
http://developer.android.com/guide/components/services.html
Otherwise if you are looking for a visual component that don't create new windows but refresh the old one, you could use the Fragment class if your android version is 3.0 or higher.
http://developer.android.com/guide/components/fragments.html
In an Activity content view, create an empty relativeLayout.
As mentioned in previous answers, create fragments and replace this relativeLayout with the newly created fragment using FragmentManager.
FOr reference use this.
Its really easy. try this.

Is it possible to recursively use the same activity using intents?

I am at a point of code where I need to use same activity with different values. Is it possible to use same activity recursively?
Yes, its possible but you have to use the condition inside the activity based on the value pass through intents.
It may possible for the same activity with different values so the value pass from previous activity will decide what data will display..
Yes.you can use the same activity but make sure,you don't go into infinite loop nor you get messed up with different codes for various conditions you call the activity.you can achieve that by maintaining extras in Intent you use to call the activity.
Wouldn't it be more suitable (in terms of readability) to create a new Activity-class with the same layout but other functionality? This way one Activity will have one behaviour instead of two - making your code more simple, readable and maintainable.

How to stop an Activity from parent Activity?

_alerts = new Intent(homeActivity,Alertss.class);
homeActivity.startActivity(_alerts);
...
...
...
homeActivity.stopService(_alerts);
I need to stop the Activity i created from the object where i created it. I tried it in the way above but it did not work. Can any one suggest me a way to do this.
You can use Activity.startActivityForResult() and then Activity.finishActivity() passing in the same result code you used for starting it.
However, this does sound like something that would be better done just as a Dialog.

Re-use previous activities?

I have activities that are create and launched from menu options. However Ive noticed that this may mean that sometimes there are two or more copies of the same activity. So Im wondering if there's a way to see if another activity is already instantiated and then have the application switch to it or create a new one if its not instantiated.
You can control some aspects of this with android:launchMode on the activity.
Programmatically try following:
Intent intent = new Intent(contextActivity, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
contextActivity.startActivity(intent);
You can specify information regarding that in the android manifest. See activity element documentation. I believe that launchmode might control what you are after.

Categories

Resources