How can i switch main activity not in manifest? - android

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

Related

Too many Activity in the stack make the app pretty slow

Recently I created a social app. I didn't use fragment and the project is almost finished. I have several Activities like UserProfile, Followers, Followings activity.
Normally it's just working fine. But if user click UserA UserProfile activity -> and then click A's Followers -> select UserB Userprofile activity -> click B's followers activity -> select UserC Userprofile activity....
In this loop, the app would get pretty slower because it opened too many activities at same time and the back stack hold all of them.
I just wonder if there's any optimization I could do for this situation? Because UserProfile activity layout would always same except the user information content. Is that possible to use Fragment for each activity, even though different activities would show up in sequence one by one?
Thanks!
You should architect this in a different way. You should only ever have one UserProfileActivity in the stack. If you already have the UserProfileActivity for User A in the stack, and you want to show the UserProfileActivity for User B, just call startActivity() for UserProfileActivity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and pass some extras to indicate that the Activity should show User B. Use the same concept for all of your activities.
To make sure that the BACK button navigation works correctly, you will need to override onBackPressed() and figure out what Activity needs to be shown and with what data. Then call startActivity() and also set Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and provide extras so the Activity will show the correct data.
To assist in keeping track of where you are in the navigation, you might want to create a stack of items that are stored in a static variable somewhere. Each item would indicate what Activity is being shown and with what data. Every time you launch a new Activity, you push a new item on to this stack, and every time the user presses the BACK key, you pop the top item off the stack and then look at the one underneath it to determine what Activity to start and what data to send in the extras.
With this scheme, the user can click around all day long and you will never have more than one instance of each Activity, but the user will still be able to navigate all the way back.

Android - Activity Stack/Tasks with Navigation Drawer

I have three top level activities in my application. Activity A, B & C.
Each one of these activities hosts a navigation drawer. I am trying to figure out the best way to manage the activity stack between these three activities.
For example, When I start the application, Activity A is launched.
Activity A has a navigation drawer like Activities B & C. When I click on Activity B in the drawer, Activity B is launched and clicking on Activity C in the drawer launches Activity C etc...
I don't want to finish these Activies when the drawer launches a new Activity because they load data from a backend service, and when I click the back button I want it to send the application to the background.
Essentially, I am looking for a way to launch the activity if it does not exist, and if it does, just resume it. How can I accomplish this?
I think decoupling retrieving data from the activity is the best option.
The following paragraph is from Tasks and Back Stack:
Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state).
So in your case, grabbing the data in the background when the app starts using async tasks and storing them in the database might work out better.
One way to do it would be:
On create of the home activity, quickly grab the home activity's data via async task while showing a progress bar. When done, store it, and display it. Then, launch async tasks for the data for other activities. There are some conditions that could be tricky. For example, you have to make sure you show a progress bar if the user quickly switches to Activity B or C before your data is ready.
Perhaps using a singleton might suite your needs if you do not want to use the DB. Depending on the size of your data, parceling your data and passing it through a bundle might also prove to be a good technique.

How to check if fragment was created for the first time

I would like to check if a fragment was created for the first time so that I can launch a different fragment for introduction before coming to the retained fragment.
Normally for an activity I know I would use a sharedPreference-object to store a boolean value that tells me if this is the first time the user opens the activity, check the preference when the user starts the application, and if it returns true then show the middle screen.
Is the same possible for fragments?
Yes, you can use sharedPrference, but maybe it will be better to check the state when you actually switch to this fragment.
I mean you can decide in activity if to show introduction fragment or regular one before you create the fragment.

Android place activity on another activity

I have two activities in my app, FirstActivity and SecondActivity. FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
One of the thoughts I had on how best to accomplish this would be to open SecondActivity on top of FirstActivity, and then remove SecondActivity, when the user wishes to go back to FirstActivity. That way FirstActivity doesn't have to reload because it was loaded the whole time, merely hidden behind SecondActivity.
However I'm not really sure how to best accomplish this task. I couldn't figure out how to open an activity on top of another activity. Any help on how best to do this would be greatly appreciated, thanks!
FirstActivity accesses information from the internet so it takes a few seconds to load initially. I'd like to be able to open SecondActivity and then easily go back to FirstActivity without reloading it.
I recommend that you start with the 2nd activity directly, and from there you begin an async task to download whatever you need from the internet. When the async task is done, you simply switch to the first activity, which uses the things you downloaded. If you have few things to pass over, consider putting the downloaded info as an extra to the activity switch intent.
Alternatively, look into fragments:
http://developer.android.com/guide/components/fragments.html
Have your two current activities be two fragments. For the main activity, set the 2nd fragment as active at the start of the app. When the download is finished, switch to the 1st fragment.

Android Multi-Screen Application

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...

Categories

Resources