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.
Related
While peer reviewing a colleague's code I noticed she created a new Activity and all functionality is just there without a Fragment.
In the old days of Android, this is what we did, but the last few years I and my peers always took the approach that every Activity should have at least one Fragment and no actual code should be written in the Activity apart from loading the Fragment of-course and maybe some higher end procedures.
I want to argue for always using at least one Fragment in every Activity, but I couldn't find compelling arguments about why it is better than a no-fragment Activity.
The out of the box argument I can think of is that it will be easier adding new fragments if needed, but if we know this will never be a necessity, why bother with a single fragment Activity?
Fragment is easier to extend and test, if you are writing another new feature, it is helpful for separating code. And you can also move your fragment code to another place easy.
Of cource, if you are sure that your code is very simple and stable, like demo or temp test code, you can also use Activity without fragment.
Since the new upgrade I have no idea on how to navigate with fragments.
A lot of code from here is commented out. If I use the code that is left, it doesn't work. I get the activity not found exception. I use the same headers as written in demo example. What are the steps I need to follow to create a succesful fragment?
The new fragments are actually pretty straight forward. All you have to do is annotating your fragment classes with the MvxFragmentAttribute and then making sure that the type passed as the first argument of the MvxFragmentAttribute implements IMvxFragmentHost, so that it can handle the Open and Close methods used by the MvxFragmentsPresenter.
The new MvxFragmentsPresenter will handle whether or not the current top Activity is responsible for showing the requested fragment or not and, if it isn't, it'll start the proper activity in before showing the fragment.
I came across this article but this article suggests replacing the FragmentActivity's code with that of the old Activity code. I can do that but would like to know if there's a way to just create Activity instead. I updated to ADT v22.6 today. Could that be the problem?
Another problem is that this requires a min-SDK of 7 while right now I want a min-SDK of 3.
Also, I am using Eclipse and not Android Studio.
Thanks in advance!
The new ADT 22.6 has new feature added that it will automatically create the any new activity as FragmentActivity not an single activity. So if you wish to create an Activity you can create it by explicitly creating separate class and extends Activity.
I'm afraid that google is enforcing us to use fragments activities instead of plain activities now. You either can go back to an SDK previous to 22.6 or just create the activities by just extending the class, adding a layout and adding the activity reference to the manifest.
The android developer tutorials recommend me using the host activities of fragments to pass around data and whatnot, so why is there a set/get target fragment method?
My application thus far contains a host activity, and a fragment, which has a button that launches a DialogFragment, in which there is a button that launches ANOTHER DialogFragment. Using setTargetFragment/getTargetFragment has made this whole ordeal somewhat confusing though, so I am thinking of reimplementing to let my main activity handle DialogFragment.show methods through my main fragment's custom interface.
Is this the right way of thinking? Or is there no harm in using setTargetFragment? Can anyone provide good and bad examples of using it?
Also, you may end up with exception of no target fragment found in fragment manager. This happens if after rotation (or other config change) your target fragment will not be readded to the fragment manager by the time when caller fragment will be adding.
Imagine you have some sort of Confirmation fragment which you add from MainFragment as so:
ConfirmationFragment frag = new ConfirmationFragment();
frag.setTargetFragment(this, 0);
getFragmentManager().beginFragmentTransaction().add(R.id.container, frag).commit();
Now on some confirmation button click you invoke a method from MainFragment by calling:
((MainFragment)this.getTargetFragment()).onUserSelectedGoodButton();
This is pretty and simple, but if you will rotate the screen and for some reason ConfirmationFragment will be added to FragmentManager before MainFragment, exception will be thrown, stating that target fragment is not found in the fragment manager
I don't think there is implicit harm in using setTargetFragment, however, I would only use it in very specific circumstances. For example, if the target fragment is only going to ever be used by the fragment (taking into account object reuse and designing your classes to be reusable when possible) and even then, sparingly.
By using them too much, you will end up with what you're seeing - confusing code that is hard to follow and maintain. On the outset, by marshaling everything through your activity you maintain a "flat" hierarchy that is simple to follow and maintain.
I think the decision to use setTargetFragment or not is a coding-style/philosophical one that, with wisdom and experience, it "feels" right or wrong. Maybe on your case, by evidence that you are questioning your older code, you are gaining that wisdom :)
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