Is it possible to change something in a view depending on the sequence of activities? For example, in an adapter I would like to highlight a text, corresponding to an element clicked in a list of elements of a previous activity. But this, only if this adapter is the result of a sequence of certain activities, given that it is used in a fragment, hosted by an activity that can in turn be called up by one of two other activities.
You can keep history of every activity opened by keeping a list/stack of ids (integer or string). First activity that starts in your app will make a new empty list and record itself into it. When another activity is opened you pass that list in the intent. Next Activity extracts the list and adds its own id in it then passes it to next Activity in intent. This way, any activity will have a history of all activities opened previously.
If you want to send additional information along with ids you can make a Parcelable class and pass that along.
Related
I've an issue with passing a variable back to the first activity.
My app starts with one activity, which opens a second one, which opens an third one, which opens a forth, which opens the first activity again.
Now I want to get a variable, which I get from a user input in the third activity in my first one. I already managed to pass variables between two activities there and back with onActivityResult() but I do not get how to manage this between more than two activities.
use bundle
you can use Bundle for move the value from first activity to second activity
check this link ---> [here] (Passing a Bundle on startActivity()?)
if use value in several activity you can use SharePrefrence or you can make
class extends Application and make value in the class and use the values in several activity
be careful if close the app destroy the values
You can use shared preferences to access variables in all your activities or use can use this method:
When going from fourth activity to first use startActivity(intent) and add the variable as an extra in intent. And in first activity override onBackPressed. This may not be good practice but it works.
I have general questions about BottomNavigationView. I would like to have a BottomNavigationView in each of my Activities in an App for ordering something (e.g. food). It should have 4 buttoms:
Back
Info
Stats
My Orders
With 'Back' the app should just go back to the previous activity. The buttoms 'Stats' and 'My Orders' should switch to a persistent activity that should not be destroyed when not being displayed. 'My Orders' should display the last orders. The buttom 'Info' should only display some information about the current item or current menu (depending from which activity it is called). So basically I have 2 questions:
Should the Activities 'Info', 'Stats', and 'My Orders' be real Activities or just Fragments? Normally I think that at leat 'Stats', and 'My Orders' should be real Activities as they are persistent. But in many BottomNavigationView only Fragments are used?
How can I pass content information to the Activity/Fragment 'Info'. This Activity/Fragment should display information based on the Activity is was called from. Let's say the Activities are different dishes. Do I have to create a separate Info-Activity/Fragment for each dish? Or can I somehow define a dynamic Activity/Fragment that displayes information based on the current Activity?
I'd appreciate every comment and I'd really appreciate your help.
The recommended approach is Single Activity and Multiple fragments.
You can do this using Jetpack's Navigation Component
In case you need to pass data from an Activity/Fragment to the new calling Fragment, it can be done by setting arguments on the calling fragment and then getting it on the called fragment. If there is something which requires to be dynamic, for example- dishes fragment, make a single fragment and common layout and load the data dynamically from the backend.
For Setting Arguments, this should help
How to pass a variable from Activity to Fragment, and pass it back?
Note: You can use fragment without using Navigation Components but you have to use FragmentManager and FragmentTransaction and also have to maintain the Backstack by yourself which could be quite complicated
Let's say I have two activities/classes. MainActivity and SecondActivity. For each of these activities I have individual XML files. In SecondActivity, I have a ListView with ID listview.
How can I retreive the ListView from SecondActivity in MainActivity?
I tried just plain and simple (in MainActivity.java):
ListView listview = (ListView) findViewById(R.id.listview);
But that crashes my app when I try to set an adapter on it (nullPointerException). I tried placing it directly inside my class, and inside the onCreate method. Both causes a crash.
The line that causes the crash looks like this:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(adapter); //crash (nullpointerexception pointing to nullobject)
What I want to do is get that ListView and use the "listview" object to add elements to the listview in my MainActivity.
In Android (with exceptions) you cannot have more than one activity running at the same time. You can have multiple Fragments, but a single activity is shown at a single time (ok, this is not 100% true anymore, but for all intents and purposes, this is still the case).
What you're trying to do is considered extremely bad practice in Android (and iOS for what is worth).
What can/should you do?
Your requirements appear to be some form of: I have a list of items, and I also have another activity where I want to add or remove things from said list.
This is fine, but you don't need "two activities" to do this.
Have one activity to show the list, and when you want to add the contents, either launch another activity to show the UI of the "edit/add" or use Fragments (one Fragment would have the list, the other would have the UI to edit the list).
Where is the data in all this?
Stored in a THIRD place (let's call it "YourListRepository"), which is a class where:
From the "List" you'd simply say: Repository, give me the latest list!
From the "Edit" screen, you'd say: Repository, give me the item "N" from the list. And then, you'd say: Repository, here's the updated item "N", refresh it.
Going back to 1 (to display the updated list) is automatic, since it asks "for the latest list".
Where to learn more?
Given the tenure of the question you posted, it appears that you're relatively new to Android development, and therefore I suggest you spend an hour of your time, reading Application Fundamentals from the official Google documentation.
I'm currently writing an app to provide food information/a grocery list function. My current code uses a switch statement to determine which button (fruit, vegetables, etc.) was pressed and passes an array list to a fragment that is then displayed as a listview.
When its called by the information activity, users can click on items and be presented with a detail view. If the calling activity is the grocery list activity, users can swipe to add to an empty array list. Since the bulk of the code used is the same, is there a way to do this without using two separate fragments?
All I can think of is putting the activity hashcode in the bundle being passed
to the fragment and using if statements to determine which activity passed the bundle (and thus which event listeners are used), but this doesn't seem like a very good solution to me.
I am making an android app. this app consist of list. by clicking each item of list a new page opens.
this list contains 50 items.I should make 50 activity and their corresponding XML file.
so, is there any way that make this process easier that don't force me to make all this 50 activity one by one?
my reputation is not enough. so I upload related picture.
http://uupload.ir/files/oh1o_1.png
http://uupload.ir/files/zam9_2.png
Not sure what you mean by creating 50 pages. You have NOT to create a new class for each item in the list, instead you will create a new instance of a class. This is what programming, and OOP (Object Oriented Programming) comes into play.. Basically what you need to do is:
Create a "main_activity" class which will contain your list
Create a "item_detail_activity" which will show the details about your clicked item
What you need to write is the logic of "passing" the correct data depending on the clicked item. When an item in the "main_activity" is clicked, you will create a new instance of "item_detail_activity", passing the correct data (through a bundle).
BTW there are a lot of tutorials out there that will help you understand better the logic of an Android application.
If you have a list of items probably means that you have a list of objects which are of the same kind with same structure.
Due to this, you will have 50 pages which show to the user the same item with different values to variables, with the same structure.
You can make the objects in your list parcelable (refer to this), then pass it with the Intent to a second Activity you create, receive it, and at last populate the screen with the item you passed.
If you have troubles or doubts feel free to ask :)
EDIT:
Imagine your listview is called ListView, and the List of items you used to fill the listview is called list in the MainActivity do this:
ListView listView = (ListView) findViewById(R.Id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position is the index of selected item
// Launching new Activity on selecting single List Item
Intent i = new Intent(MainActivity.this, SecondActivity.class);
// sending data to new activity
i.putExtra("item", list.get(position));
startActivity(i);
}
});
in your SecondActivity, with getParcelableExtra("item") you retreieve the item clicked. Here with the variables of this item you can populate the page.
here is the doc for intents.
In secondActivity, if something must disappear if a variable has a certain value or is null, play with visibility or fragment: create an Activity with all case shown, than work with visibility or with fragments and adjust it ;)
Change your activity to fragment activity and write code to display the details corresponding listview click. it easy to follow.