Android: 2 different Views means 2 different Activities? - android

My current application has one Activity, the main one which extends ListActivity (listview of course).
The main Activity also holds most of the app functions.
I want to add an option for the user to select between 2 views: the current listView and a gridView.
I also want to reuse my code - most functions can be used for both Views.
Before I ask the actual question, note that my question is NOT how to add a gridView - it's already added and works great (when commenting out all Listview lines).
My question is - how should I do it?
2 different Views means I have to have 2 different Activities? one extends "ListActivity" and one extends "Activity"?
Should I create 4 classes (3 Activities and functions class in this case) where the main Activity will call one of the other Activities according to what the user selected?
Is there a way using the same main Activity for both Views? (right now I extend "ListActivity" to get "getListView()" which causes the gridView to force close as the Activity looks for a ListView with the ID "list").
Any other way?
Thank you!

You can use ListView without using a ListActivity. I would recommend this approach if the rest of your Activity's code will be similiar for both the List and Grid forms. Choose dynamically which one you show and tailor your adapter definitions to do both based on a switch or if statement where needed, rather than using different Activities.

Related

Reusing the MainActivity in Android

I have a MainActivity in my Android Application where I am showing a list of items in a RecyclerView.
Each Item can have multiple sub items, and sub items can have its own child items too (like a tree).
I want to show the same activity for main and sub items, but with an Back Arrow on ActionBar.
Is it possible to reuse MainActivity?
yes, you can do this using fragments. Instead of showing lists in MainActivity take a fragment container in MainActivity and create fragments where you can show your lists and subitems according to your requirement.
By checking fragment instance in MainActivity, you can show and hide backarrow of actionbar.
Check this
Yes, it is possible to reuse MainActivity. Each time you call startActivity without special launchMode or flags, a new instance of MainActivity will be created and it is considered an absolutely new one.
I have encountered a similar problem while working on a File Manager application. So What I did is created a FolderNavigator stack that helped me in keeping the track of users current position using which I showed breadcrumbs at the top. And I hooked my fragment with this navigator so as soon as the peek of stack changes my folder gets notified and it will load the data of the peek of the stack. When I press back I will just pop the stack and as my fragment is already hooked with the peek of stack it will show the respective data.
You can use fragments to fulfill your requirements.
From this SO answer:
Fragments are more of a UI benefit in my opinion. It's convenient for the user sometimes to see two different views of two different classes on the same screen.
For more about fragments, read this document.

How can I change one page to another without using Intent?

I want to implement quiz module in my application which consists 14 questions so how do i go from one question to another. If i use intent then this is not smart work because i have need to create 14 activities. is there any other ways to move to next page without using intent.?
You can use the Fragment inside your activity which load your view with data. For this you need to ID to get the data for example Question 1 have ID 1 which will be fetching from database and display in your view, this will be implementing in your fragment, firstly get the ID and query in your database for fetch that record and then display in your view and this fragment object will initiate and set in your layout. Now after question complete pass the next question ID to your fragment with new initialization and replace with current fragment.
You can achieve this in two ways
1) Using Fragments:
Have 15 fragments
All are mounted on a single container
Start one fragment to another dynamically
2) Using a Single activity
Have 1 Textview for question
Say if you have objectives answers have radio buttons for them
Then u have to write a logic to dynamically change the data one after
another keeping the same activity
Best Approach
Using the Fragments Would be best and simple way to achieve your objective because second option unnecessarily complicates the things
Online Sources to learn fragments
If you have not used fragments before click here to check developer
docs
Simple tutorial to learn fragments
I assume your modules are in different layouts. Then you can use above syntax to change to another one:
setContentView(R.layout.second);

Start activity inside ViewFlipper or a layout

I have a layout with tabs on top (added programmatically) and ViewFlipper under with 3 different views one of which contains a ListView. And I have 2 activities, one is the primary one that creates the tabs and one that dowloads some data and displays it in a new ListView. What I would like to do is start the second activity from the first one and display the data in the ListView that is inside ViewFlipper. Is it possible to do that?
What you are doing may be easier to achieve using fragments, or by putting the second activity into the ViewFlipper completely.
See the Android Developer site here for information on utilising fragments:

Android Layout decision and its implication on performance

I have two layouts one is main and other is listview used when user clicks some button. Which is better in perfomance, to use two separate activity for both layouts or have to implement both in same activity.
2.And my main activity extends Activity but to display listview i need to extend ListActivity so if i extend ListActivity app is forceclosing , may be cause I'm tryin to load linear layout first. So what's solution ?
There is no definite answer regarding performance. Having a simple activity perform two actions may impose some runtime overhead, while having two different activities will imply an increase in code size. If I were you I wouldn't bother about performance, and I would choose whatever solution is better design-wise (probably creating two activities, but you never can tell).
Note that extending ListActivity is not needed in order to display a ListView in an activity, its just a convenience. If it weren't, then people would have lots of trouble trying to come with activities displaying more than a single ListView.
I think, using a single activity for both the tasks will be better. You can perform the task of displaying and loading data in ListView in a separate thread avoiding the UI to stuck...

Converting Multiple Activites into a Single Fragment

I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.

Categories

Resources