3 Activities in Android Application - java

I made 2 Activities and started a second activity by clicking on a Button. All is good...
But if I add third activity - nothing works - I can't start neither the second activity, nor the third activity...
Program scheme:
First Activity has a Button, which starts the second Activity
Second Activity has a Button, which starts the third Activity

There shouldn't really be a problem if you have written it correctly. I recommand taking a quick look on the tutorial about creating a basic program in the training guide released by the Android developer team. They give you an idea on how to make the first button, just add this method to your second button instead of receiving text back.
As mentioned above, we need more details and you should also check your manifest file.
There isn't really much more we can help you with when you give us these details. Sorry.

Related

I don't understand how to organise my fragments

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.

How would i resume the last viewed activity after the user has been closed the application?

How would i resume the last viewed activity after the user has been closed the application. It is like, If the user open the application, the first activity will appear and when the user click the button it will proceed to the next activity, if he close the application and open again it will still show the last viewed activity. how would i do that? please help me.
Thank you,
I think you should use sharedpreferences. And in every activity you open, you have to put a value on your sharedpreference so that if you open again your application it will check the value of your shared preference then compare it, call the activity that fits the value you got. Hope it helps :)
You need to track his last activity then, keep it in a database and whenever he returns, look up his last activty in the database...the other way is cookies but not adviseable ....
Place this below line in AndroidManifest.xml for your activities in <activity/>.
android:launchMode="singleTask"

Multiple MainActivities start on startActivityForResult() in Android

guys,
I've been working on a native Android application for some time and now I am at the end of it there is still a problem I need help with.
My project at github
There is the following problem - when user starts the application for the first time the app will ask him "When does your diet cycle starts?" with a datepicker popup. I am using SharedPreferences to store the result user has picked from the popup. I have separate DatePickerActivity from the MainActivity one that takes care of this datepicker that I start with startActivityForResult(). The DatePickerActivity passes the result to the MainActivity using an Intent.
When I debug the app I see finish() in DatePickerActivity is started twice and the MainActivity is started more than one this cause the datepicker popup to show once again.
Once the start date is set in the system there is no problem with these activities and application works fine.
Then comes the moment when user wants to reset the date - using the basket icon with text like "Изчисти" and the datepicker once again appear twice.
I hope I've been clear enough with my explanation and I am looking fowrard to hearings from you.
Best Regards,
Mihail
I managed to find a work around my problem. I guess I caused it because I need my main layout redraw after user picks a date from the picker and I doing
finish();
startActivity(getIntent());
Here should be the reason for multiple MainActivity instances in the app. Second thing I guest is that committing SharedPreferences to the OS is async and by the time I check for selected date it has not been written yet.
I solved the problem by saving picked date in static instance and when I redraw the main layout I already have the result.
Thank to those who lost some of their time trying to fix my problem!

Android application design help, How to control which activity shows

Hello I require some advice on how to approach this problem,
bear with me as I am still new to android.
I have an activity that opens on application start and requires some information from the user, but next time the user opens the app, i want the application to open a different activity that will display the various information.
Kind of like facebooks app, where when you first run it, you have to login, and only then next time you run the app you are guided straight to the feed.
Any ideas how one could do this efficiently?
UPDATE: Ive stored the information via shared preferences and am now using a controller activity that decides which step to take.
So, Controller activity runs on start up, and decides whether to show a log in screen or whether to go straight to the information. But now im encountering a problem where i end up opening a blank activity (the controller) and then another ontop of that ( the decided activtiy). I dont want the blank activity to show, so its kinda of like a background process, any ideas?
Ideally you would have a main activity like a controller. Use a SharedPreference object to keep track of whether the user is logged in or not. So back in your main activity, read this value and if it is set go to your news feed activity else show a login screen activity. (as you do the check and redirection, you can show a progress dialog)
links for SharedPreferences
MobTuts
Android Developer

Android ending activities

I go through few activities and when I want to go back to main window I use FLAG_ACTIVITY_CLEAR_TOP but if I press back button then I would go back to other activities that I went through.
Briefly speaking I want to go to main activity and after presing back button it wouldn't go to other activities.
EDITED
This clossing all activities is used to complete logoff process in another words it shouldn't user let to go back. I can't use BackPressed(), because I am using putExtra and getExtra. Maybe someone could write small sample. Because I don't understand flag system.
Use FLAG_ACTIVITY_NO_HISTORY on Activities you don't want saved on the stack. When the user presses back he/she will go to the last activity that didn't have that flag set.
You can override onBackPressed in your activity and start your desired one with the flags:
FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP

Categories