Android Multi-Screen Application - android

How do you handle multiple screens in an android application? I have developed with the tab bar at the bottom without problem, however what I am wanting to do is replace all the content on the screen with the content from a new .xml layout file I have created in the project. Additionally, how would I tie the back end code to the new layout file? I'm sure this question probably exists already and is googleable (may have made up a new word). However, I don't know exactly what it is that I am looking for. Thanks in advance for your help.

What you need to do is, create a new Activity and add it to the AndroidManifest.xml:
<activity android:name="ActivityClassName" android:label="Label for the Activity"></activity>
and can be called in a method:
public void startActivity() {
Intent someName = new Intent(CurrentClass.this, ActivityClassName.class);
startActivity(someName);
}

Android applications generally use a separate Activity for each screen, and switch between them using Activity.startActivity and Activity.startActivityForResult. You can pass arbitrary data to an Activity via Intent.putExtra.
Hope this helps,
Phil Lello

It really depends on how you want your application to flow.
Let's consider the scenario where a user does the following:
Starts your first activity
Presses the 2nd tab
Presses the 3rd tab
Presses the back button
If you use a separate activity for each screen, then the following would happen
Activity 1 is started
Activity 2 is started
Activity 3 is started
Activity 3 is closed, user returns to Activity 2
(in this case pressing the back button again would you take you back to Activity 1, and pressing it again would exit your application)
If you used one activity for all the tabs, then the following would occur
Activity 1 is started
Activity 1 sets tab content to tab 2 content
Activity 1 sets tab content to tab 3 content
Activity 1 is closed, user returns to home screen
If you are using a screen with tabs, then the second method (a single Activity with a TabHost or similar) is the preferred method, otherwise the user will end up making a large activity-stack just switching between tabs (meaning if they switch between tabs a lot they'll have to press the back button a lot of times to exit).
If you want to go for the single activity approach, then do some research on TabHost and TabContentFactory. In the createTabContent method of your factory you can inflate a View/layout from XML to set as the tab content using View.inflate. Look those up and come back ask another question if you get stuck ;)

i think you may want to play with more than one activity.... you can have multiple activities and one xml for each of them... in this way you can have different screens... check these links. Multiple Activities, Creating an Activity.... hope this helps...

Related

How can i switch main activity not in manifest?

I making android app. I have 3 activites:
first(main) activity which is empty and it's like navigation which ask you to add item.
Second activity which is needed for adding item's details and this activity have accept button which leads you to third activity.
Third activity have all data about item and from now on i want my app to make this activity main (default) activity with saving information about it in device memory.
I decided to make it without database because it should be really simple.
Can you help me with making 3rd acitivity main? Maybe there are some tricks.
not clear what you trying to ask. Like if user reached the 3rd activity, in the future if user open the app the app will automatically navigate them to the 3rd activity?
If that's the case, you could save this information in the shared preference, when the app start, the first activity check this value and navigate to the 3rd activity.
Recommend you use a single activity, use fragments for different pages, and use Navigation component to connect them. https://developer.android.com/guide/navigation/navigation-getting-started

Best approach for accessing activities from another

I'm developing a simple home launcher. Main activity is the home layout, in it there is a button to call another activity to show the apps list. In this second activity, if you long press an icon, it adds to home screen.
So, from the second activity (apps list) I add an imageview to the main activity (home screen), but to do this I need to get the main layout, which it's not possible from different activity than itself.
To achieve this, I'm thinking different options like this:
1-Declaring a variable in the second activity:
public static RelativeLayout mainLayout;
Set it in the first:
// MainActivity
Intent i = new Intent(this, DrawerActivity.class);
DrawerActivity.mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
startActivity(i);
2-Using fragments avoiding the jumps between activities.
3-Changing the layout in the main activity (one activity for two layouts).
4-Declaring class descending from Application and store here everything I need in the different activities.
5-Use of broadcasters.
The question is: which is the right approach to achieve this ?
I've read several docs but there is no clear answer.
Definitely don't go with option 1. That's the best way to leak memory and context, and you don't want that. Resources associated with an Activity (in this case the layout) should be private to that Activity. This ensures that the framework can manage memory as it was designed to.
To communicate between activities, the official way is to pass parameters in the intent that launches the activity. You can add the identifier of the application you want to add to the main screen, and than retrieve the image either from the intent (as seen in the link) or if second Activity is launched for a result, then in the onActivityResult method of your home Activity.
However in you case I would suggest another approach. As you have to persist the layout of the home screen, I would create a database table containing the positions of the applications added to the main screen. I would modify the database entries where it's convenient and rebuild the main screen's layout every time it is displayed based on the database.

How to change Activity in Android with Xamarin?

I'm new with Xamarin and I'm tring to create an Android App.
I have created a 2 Views related to 2 different Activities. The first Activity, name it A, has a button that launches the second Activity, name it B.
B has an EventHandler that is connected, in the OnCreate method, to A's Event. The EventHandler print on console a string, that's it.
if I launch the app and press the A's button, the B activity appears. Now, if I press the Back button the A Activity appears again, after that I press the button again and the B Activity appears but this is a new B Activity istance and not the previous one. I can see that because the OnCreate method is called twice and because I can see on console that the event is called twice.
If I repeat this many times I can see the same string printed on console repeated many times. I would like to have only one instance of any activity, so I need to change view without creating more istances of one activity that was already created or destroy the B Activity when the Back button is pressed.
How can I do that? is it the right way to do it?
You can't link the two activities, or better, you really shouldn't. If you need shared data then go through static properties (perhaps in a separate static class).
Read about activity lifecycle and activity launch modes. Note that you are probably creating a wrong flow though.
I think in order to get what you want (having two "Activities" of which the instance is always the same when you are switching between them), you basically need to use Fragments instead of Activities, in a FragmentPagerAdapter.
http://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/
FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

Back 3 activities and fields not blank android

I have 3 activities and want it to be possible to fill in a form on the activity after a 1 button takes me to the second activity that calls for his third activity. In the third activity I choose a value and want to go directly to the 1st activity but want the 1st activity is in the state it was in when it comes out.
I have this in 3th activity:
Intent selectFavorite = new Intent(view.getContext(), FirstsActivty.class);
selectFavorite.putExtra("Data", Info);
selectFavorite.putExtra("SUB", favoriteListArray.getJSONObject(position).getString("sub"));
selectFavorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
When i return the fill is blank :s
I am 99% there is a different and more reliable way to accomplish what you desire. If by any chance the reason you are going to SecondActivity and ThirdActivity is to get some input values for the FirstActivity's form, then you should consider doing this not in a different Activity, but using dialogs or similar. That way you will avoid having to unnecessarily manage more activity's lifecycle overridden method than what you actually need.
If you agree with me and you don't know if you should use dialogs, or action bar menu items or similar for what you want to accomplish, try checking out the Android design guidelines: http://developer.android.com/design/building-blocks/dialogs.html

android - How to Exit from the application

My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.

Categories

Resources