I don't understand how to organise my fragments - java

I have realised an NFC reader application
So i have 3 activities :
MainActivity, which is an activity who contains a Button. If button is clicked, the scan is activated and the user can put his NFC tag against the device to detect it.
WebActivity, who is launched if the NFC tag contains and URL (and open a WebView) or if the user want to launch WebActivity by himself
HistoryActivity, who gonna contains a list of every scans.
Now, I would like to swipe activity with a finger gesture. according to my research on Internet. I need fragments and ViewPager.
But every example that I saw is bases on ONE activity and multiple fragments.
But in my case, I have to create 3 fragments (one per activity), right ?
And I really don't know how to manage my fragment. I mean, what to put inside ?
All I want to do is create a transition/animation while changing activity... That's crazy

This is too broad of a question but hopefully my answer will steer you in right direction.
You should definitely go with single activity/multiple fragments model. Aside of recommendations by Google, you could use navigation components, deep linking much easier then without single activity.
Yes you should be using ViewPager for the purpose (and likely your implementation of FragmentPagerAdapter as well) however I do not understand what kind of swiping will you be doing
Reading your setup, I would suggest to use bottom view with 2 items (good example is here https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample). 2nd one would show history, first one would offer a button that activates your action, and then displays fragment with your WebView.
As a side effect of such implementation, you'd be able to go back from 2nd bottom view item to whatever first one holds - by pressing system back button - which I think is nice touch.
UPDATE to "swiping takes place anytime. " comment:
You could have single activity, ViewPager with 2 fragments. First fragment would display a button, 2nd fragment would display a history. You could freely swipe between them, as you want to. However to me it does not make sense to put WebView screen into this. WebView screen is result of action (NFC detection) and it should probably display as full screen, without any chance of swiping between main/history and itself. Hope it helps or I'm missing some important piece of info you did not share.

Related

What are the general instructions to add a new fragment that uses a new screen and has its own layout?

I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html

Connection between activity and layout? How to change layout? How to start and destroy an activity?

I'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android:
The activity does all the work "behind the scenes". Activities are connected to a layout in the XML file. When the activity is started, if declared in setContentView method, the connected layout will be shown. Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout.
Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements.
Am I right with this one?
For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml. Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml? Is this correct? Is there any better way to do this?
How can I (by which method) destroy/stop a certain activity?
Thank you in an advance.
The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html
I will answer your specific questions here though
An Activity is a window that the user can see (or a hidden window if there is no layout defined). It deals with the logic of a part of the app that the user can see and interact with. If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout).
If you want to show a new window/screen/activity you do not need to destroy the current one. You can open a new activity whilst keeping the old one in the background (in the back stack). With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html.
This point relies heavily on the activity lifecycle. When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background).
When we say an activity is stopped, it means that the activity is no longer visible to the user. This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background.
This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge.

Keep d-pad focus changes within current fragment

I have an android app that primary receives input using a d-pad (up, down, left, right, OK, back)
The app runs on a TV so uses fragments to separate sections.
I would like to keep directional buttons from focusing views in other fragments. And handle directional buttons differently depending on what fragment is set as the "current"
What would be the most elegant solution to this?
also, Fragments are dynamically changed at runtime.
When the d-pad is the only means of navigation for a user, such as on some Google TV's, then it's not a good idea to stop the navigation at the edge of a fragment. How I solved this, is by allowing cross-border navigation and laying out elements so that the navigation is not awkward and works by finding the path automatically (after all, there does not seem to be a way to statically provide the next element to focus on in each direction, when in another fragment).
Like you I'm looking for reference advice on how to deal with fragments and navigation on Google TV.

stack activity in android

Hi i am working with android , and i have a problem with the activity stack. As i know, when someone uses the back button, reload the back activity. But in the case i have many layouts shown from one activity, how can i go back to them.
Here is the deal, i am using a listview filled with categories, and when i press an item, i reuse the activity and the layout, to show its subcategories. So what i need is to came back no to the back activity, not to the back layout, but to the back "state".
Well, the idea is simple, first i show all the categories with no parent, then when i pressed an item, i show its subcategories.
The easiest way is creating two Activities - for categories and for subcategories. If you try to implement all the logic in a single Activity you won't earn nothing and just end up totally confused. Using Activities simplifies things a lot just because it handles problems such as yours. Hope this helps.
Check out Fragments, they are the stepping stone between a view and an activity. An activity can have multiple fragments and will manage their back stack (if you tell it to).
http://developer.android.com/guide/components/fragments.html
You'll have to use the support library to used them on pre honeycomb devices.

Android - 2 Activities active at the same time

I have a GameActivity. I also have a transparent ChatActivity running on top if the user presses the Chat options from the Menu (onOptionsItemSelected). The problem is, when a player starts the ChatActivity before I start the game, an odd behavior occurs and the game won't start.
Is there any way I can keep GameActivity active while ChatActivity is visible?
I fired up the ChatActivity using the normal way:
startActivity(new Intent(GameActivity.this, ChatActivity.class));
Thanks for your help.
You can't have two activities in one activity. However; one design you could possibly achieve is introduce a design that allows the user to swipe the screen to bring up the chat view and swipe in the opposite direction to hide the view.
Have the main Activity that is running maintain that view via an async process so that it can be updated as necessary and does NOT interrupt the user.
You could take a look at FLAG_NOT_TOUCH_MODAL (and maybe FLAG_NOT_TOUCHABLE), using 2 activities on top of each other, making the top one transparant and give it that flag/those flags (not sure if you can actually touch windows you create within that activity, didn't try that yet)
you could also take a look at this. It's not exactly the same thing, but maybe you could rework it a bit to fit your needs
I know this is very very late,but this answer could be of use of anyone new.
For achieving the kind of design mentioned here, you could implement fragments instead of having two activities.

Categories