This question already has answers here:
How can I save an activity state using the save instance state?
(35 answers)
Closed 6 years ago.
I am developing an application in Android Studio and want to make a favourites page. To do this, I would like the user to be able to save the activity to the favourites, and when opening the favourites page from the home page, the user can view the button to start this activity.
I have looked up numerous other similar questions but none which suits my specific issue.
I have looked at different possibilities such as SharedPreferences, ToggleButton etc. but none I can get to work.
I would really appreciate your help.
Thank you so much in advance.
May be you can create a static object of HashMap and save favorite activities to this and can retrieve activities from that. Something like this.
This HashMap Object can be kept in Application Class so you can access user list of fav activities anywhere from the App.
private static final Map<String, Class> favActionMap;
favActionMap= new HashMap<>();
favActionMap.put("FAV_1", AddFixtureActivity.class);
favActionMap.put("FAV_2", AddSwitchActivity.class);
favActionMap.put("FAV_3", CommissionStatusActivity.class);
To store the data persistently ,save it in database or shared preferences
Related
I am absolutely lost with my project and I need someone to point me to the right direction because right now im just looking at my code and I cant understand where to start.
MY app is all about answering questions. A player has to answer if the question is right or wrong. If he answers the right amount of questions he unlocks a new level. So until today I just thought I will need to create a new activity on every level, create questions in that activity and let it run. But just today I realized that having 100 activities of different levels is just stupid.
So I need to make my app work with fragments. The only thing i need is to make it that on each different level my app would select specific questions from the question list and display them with their values.
Right now i create questions in level activity. For example: Level1Activity
//Creating questions. (Question, boolean, answer).
final Question first = new Question("Do i understand this code?", true, "Only Jesus knows");
final Question second = new Question("Why dont i understand this code?", false, "Im not Jesus");
final Question third = new Question("Why I am not Jesus?", true, "2fat.");
//Creating Lists for questions and boolean values.
final ArrayList<Question> questions = new ArrayList<Question>();
final ArrayList<Boolean> type = new ArrayList<Boolean>();
//Adding questions to the question list
questions.add(first);
questions.add(second);
questions.add(third);
// Adding boleans to the boolean list
type.add(first.correctAnswer);
type.add(second.correctAnswer);
type.add(third.correctAnswer);
So what do i need to do so that I would not need to create a new activity with new questions on each level. Ho to make one activity and display them with fragments for all levels?
Probably you do not need fragments either. You can have multiple layouts (used via setContentView(int id)), or even better, have one single layout with several instances of TextView on it, and set the text in these text views programmatically.
By the way, you can startActivity() and invoke finish() on the current activity (I am not saying you should do namely this, but you can).
You must start using the same fragment in all levels, and load dynamically the questions. Using one activity for each level is a really bad idea.
First, try to create a fragment, inflate it and use it, and model ONE level. Perhaps the (parent/holder) activity can control the current level, and can pass the questions to the fragment accordingly, and save attributes such the score or the game state.
Note that from the MVC (Model-View-Controller) viewpoint an Activity is a Controller and it is wrong to keep Model data in the Controller. The View hierarchy (the View) and the Activity (the Controller) are re-created when the screen turns. Better use a separate class, probably, a singleton, to keep the data that must survive screen turns.
I want to create an Activity that configures content from all the others activities, the app I'm working on is designed for children and will contain a lot of games, but it is up to the children's parent to determine which games they will play as well as the game's configuration, so I need to create a "Game Configuration" that manages all of the activities and their views.
For instance: There's an activity called "Game list" that has an ArrayList with an ArrayAdapter, from the activity called "Config" I want to be able to acess the ArrayList and choose with games will be played when clicked in one of the buttons of the list as well as adding more nodes/Games to the list. It's kind of block-based programming.
I have little idea (AsyncTask and etc) on how to acess the ArrayList, but I don't know how to refresh it and save the modification when the app is closed. So, how do I (properly) access the ArrayList from a different class and then refresh it?
Trying to modify Activities from other Activities is a terrible idea. You should instead be persisting the user's configuration somehow (SQLite maybe, or shared preferences) and having the other activities load their content based on the persisted configuration. There is no Activity-to-Activity communication with this approach.
This question already has an answer here:
Custom camera android
(1 answer)
Closed 8 years ago.
There is an application made by another developer and I need to continue it. It has an activity with a button that calls the default camera app as an intent with startActivityForResult. It works ok when they take only one picture per app session. But now the client wants to be able to take various photos from different position of the place and then upload them all to the web server. So I need another way to do that, faster than opening the camera app for every picture.
For this I wish to load the camera directly in my activity, place a 'shoot' button, a label with the number of pictures taken, an imageView with the last image saved, and a button to finish. As an example I show you this printed screen from Internet.
The idea is to save a picture on every button click, and save the file names in an array.
How can I use the camera this way and control it with a custom button?
I also try to control the camera and I am using this link
I hope it is useful for you. It has helped me to fix several bugs
i am developing a android app which basically has 3 type of users client, driver, admin so when user install app it will launch a activity for the selection of user type means 3 buttons on it client, driver or admin. when user select a type he/she will be pushed to that specific activity for example if he selects client then he will be pushed to ClientActivity. and when ever he launches the app after first time he will automatically pushed to ClientActivity. first activity that gives option for user selection will online display first time when app install. i already did this in my ios app with use of userdefaults to save a variable and retrieving it from phone memory. so i need suggestions how can i achieve this maybe a stupid question but i am new with android development. so any kind of help will be appreciated :)
Thanks.
You can use Shared preferences,
When the activity creates check a boolean value that saved in shared preference and based on the value u can use.
For declaration:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
to save value
editor.putInt("key_name", 0);
editor.commit();
to view the data
int value=pref.getInt("key_name", 0);
based on the value u can pass the specific intent.
android has sharedprefereces concept same as userdefaults in ios. You can use in application specific context or component level context. I suggest better application level context.This shareprefereces values can primitives, objects wont support. And these values will be available for you untill application is unistalled.
I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.