I'm currently trying to develop an android application in eclipse(java) which shows some jokes downloaded from a database. The user is able to vote on each joke once, and to make sure they only do that once, I have made a table in the database that contains three columns.
E-mail
Username (Used when a user publishes a joke)
Encrypted Password
I have two "screens" right now:
Login screen
Main screen
At every single start up the application checks the SharedPreferences for a file containing some information, and if there is some information it should load the Main screen, but if there is no account information the Login screen should be loaded.
Any idea on how I can use different screens, and how should it be coded?
Two options:
In your Activity, check if there is account info. If there isn't then setContentView to the Log in screen. Otherwise, setContentView to your other content. If you go this route, you'll have to have the logic of both the login Activity and the other in the same Activity. Shouldn't be too bad if the logic is relatively uncomplicated.
Have two activities. The default Activity can be the login Activity, but in onCreate() you can check if the info already exists and if it does, simply start the other Activity right away and return from onCreate(). Otherwise, continue with setContentView, etc.
You can have a single top layout (e.g. FrameLayout) that contains two overlapping layouts (one for the login and one for the main). Use a single activity. When the activity starts do setContentView() to the top layout. Then define a method selectScreen(boolean isMain) that based on the argument sets the main layout on and the login off (or vice versa). You turn screens on/off using the setVisibility() method in class View. You can switch screen any time by calling that method. If you want to be extra fancy you can use standard animations when flipping screens.
Related
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.
I'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android:
The activity does all the work "behind the scenes". Activities are connected to a layout in the XML file. When the activity is started, if declared in setContentView method, the connected layout will be shown. Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout.
Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements.
Am I right with this one?
For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml. Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml? Is this correct? Is there any better way to do this?
How can I (by which method) destroy/stop a certain activity?
Thank you in an advance.
The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html
I will answer your specific questions here though
An Activity is a window that the user can see (or a hidden window if there is no layout defined). It deals with the logic of a part of the app that the user can see and interact with. If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout).
If you want to show a new window/screen/activity you do not need to destroy the current one. You can open a new activity whilst keeping the old one in the background (in the back stack). With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html.
This point relies heavily on the activity lifecycle. When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background).
When we say an activity is stopped, it means that the activity is no longer visible to the user. This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background.
This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge.
I have some problems with the layout and activity and I don't know are they different,are they related?
I think the layout is a place we can add or remove our views and activity is just a place that shows any thing in our layout, is this true?
An activity:
is an instance of Activity, a class in the Android SDK. An activity is responsible for
managing user interaction with a screen of information.
You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many.
A layout:
defines a set of user interface objects and their position on the screen. A layout is made up of definitions written in XML. Each definition is used to create an object that appears on screen, like a button or some text.
A layout deals with the user interface. Its where you set all your views that will be visible on the user interface.
The code behind (.java) sets the layout you created as the content view and manipulates the behavior of the views you have set. For example, sets the text for a text view.
The activity then is the whole thing, the layout and the code behind.
An activity is the java code which attaches actions and puts content to/in a layout. For this the Activity loads the layout.
Briefly,
Activity is the java part of your projects. The program and any kind of algorithms are implemented here. Also layout views come to life in an activity.
Layout is where you organize the views in your page. But without activity, they have no meaning. Because in activity, you have to get these views and use them programmaticaly.
All together, you load views from layout to activity and in activies you implement your whole program.
A layout defines all the appearance of an app and this is of no use without a java program which helps in real functioning of that visual display.
Thus we define what an app does by writing its java code and a special java class called activity decides which layout to use at a particular instant and tells the app how to respond to the user.
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
I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(), etc as outlined in a similar post ( Saving Android Activity state using Save Instance State ), but how do I save the last activity?
To be more specific, my application starts up and goes to a login screen. Once a user logs in and navigates through several activities, lets say he or she leaves the app using the home button or in some other way. Next time the user starts the app, it will go back to the login screen and do a login again. Instead, I want the app to start up and go to the last activity that was on top of the stack when the user left the app in the previous session.
How is the last activity saved so that it can be restored on app startup?
I believe Android does this automatically. We have an app that we are working on. When I click the home button and them come back to the app, it starts in the activity where I left off. We have not written any code to make this happen. It just seems to work.
My colleague has written an article on Android application state including details on getLastNonConfigurationInstance() which retrieves the instance that was last stored. Take a look here: http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx
Please note that the above answer is correct only on one case: If your process does not get killed by android because the resources (memory, ...) are needed for a different reason.
To get what you describe, I would write a custom parent activity and override the correct life-cycle methods to store your application state and read it and act accordingly. Then let all your activities inherit from MyActivity (instead of android.app.Activity)
public MyActivity extends android.app.Activity {
...
#Override
public onCreate(...) {
// Read the Application state,
// check if the currently launching Activity is the right one,
// if not, start the last Activity and finish the current one.
}
#Override
public onDestroy(...) {
// Store the current Activity ID in the SharedPreferences
}
...
}
Take care to call the super.onDestroy and super.onCreate methods in all your Activites (like you should do anyways).
Happy coding and have fun with Android!