how to reuse single activity infinite times? - java

My app has an album style feature that users can pick/take photos from device and load it in every page in this album.
Now the number of this album pages is up to user and should be unlimited. On the Other hand the codes that is being used in activities as you could imagine are exactly same. I have wrote code once and I just want to reuse this activity for each page of album. how should I achieve this?
Since there are thousands of apps that are provide this kind of functions I know there is a good way but i'm unable to find that. Just a trick or a link to a tutorial or even a small explanation is good enough.
Thanks in Advance.

You can create a next() method, and change every layout view in the page with this function, so that you are in same activity but only views are being changed.

One solution is to add the data to be displayed to the "extras" of the Intent which you use to start the activity.
Alternatively, you might want to look at using a Fragment instead of an Activity. You might also want to look at using ViewPager to be able to swipe through the fragments which display the album data.

Related

Parse different data through the activity in the same activity

I am building an E-Commerce App and I have an activity showing image slider, description and related items, and in image slider I have put an arrow to change the product on the arrow click I want to be in the same activity but data should be different.How should I do that need your help guys.
You should try doing it by urself in one of the many ways its possible to. Then we could provide some help with your idea or show you better solution. I think you are looking for RecyclerView (https://developer.android.com/guide/topics/ui/layout/recyclerview). It lets you show some similar views but with different data inside. Also as an item inside RecyclerView you can try CardView.
Using only RecyclerView you can also use:
https://stackoverflow.com/a/46084182/8713068
You can also search for a library that has everything you want inside already implemented. For example the first library that came out after i typed it in google : https://github.com/Ramotion/cardslider-android.

How can I turn 40 similar activities into one that holds different data?

I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

How to refer to a certain TextEdit in a specific Android Fragment?

I'm developing an Android app and for this I want to have a fragment with which the user can insert an amount. The fragment has a couple methods, like inserting the correct currency symbol on the basis of the country.
I now want the user to be able to insert several amounts on the same screen. As far as I understand I can reuse one fragment several times for this. Every time the fragment is used in the main xml it gets an id, and every fragment contains a couple EditTexts which each have an id as well.
I now wonder how I can get the value of a certain EditText within a certain fragment. So lets say I want to get the result of edit_text_2 from within fragment_3 (both are their respective id's). How would I do this?
You can get result from fragment via callback. Please check following documents:
http://developer.android.com/training/basics/fragments/communicating.html
Check these links out:
http://developer.android.com/training/basics/fragments/communicating.html
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
Fragments are made to be self-contained, so that they don't know what is outside of them, and their containing activity doesn't necessarily know what is inside of them. You need to create an interface for the Activity to call to get the reference to your EditText.

Android Changing activity by onTouch like a book

I am a student trying to create tales/story app. I am wanting change the activity when the user swipes right it will go to the next page and when the user swipes left it will go back to the previous page. I have been viewing examples however I am still confused how to do so. Would anyone be able to point me in the right direction?
Instead of having one activity for each page, just replace new content on the same activity.
https://github.com/MysticTreeGames/android-page-curl. Page curl.
Page curl with renderscript.
http://code.google.com/p/renderscript-examples/wiki/PageCurl. The link has a video of pagecurl demo.
The source code in available at http://code.google.com/p/renderscript-examples/source/browse/#svn%2Ftrunk%2FPageCurl.
Details regarding renderscript at http://developer.android.com/guide/topics/renderscript/index.html.
Here's a video on renderscript talk by Romain guy. http://www.youtube.com/watch?v=5jz0kSuR2j4.
You might need to use page curl effect kind of projects to achieve that like this or this . The way you think of making use of activities might not be a good idea. Because if you just make activities for each page, then it is not guaranteed that they would still exist if you come back as it increases burden on the back stack and the system would automatically destruct the activities if it requires some resources to be allotted for other things. Or you can create your own effects.

Categories

Resources