I have an Activity DetailsActivity that is accessible from two different Activities: MapActivity and ListActivity. How should I implement Up Navigation to select proper Activity as Parent?
You can set a flag while going into the details activity from any one of the activities. Then onBackPressed you can fire that particular intent depending on the flag you first sent in.
I had the same problem and in my case rearanging the code worked perfectly. If you have the same logic in two classes, rather then inheritance, try to create another class, which will do the whole work for us.
Simple add a new atribute to Details Activity and (List Activity or Map Activity).
Related
We use intents to switch between two activities and also fragments are for the same purpose. So why can't we use intents always instead of fragments?
Intent, you can think of intention to do some work. It can be either go from one activity to another activity, send email, open some links and so on.
Fragment is just like part of those activities.
To make it simple you can think of activities as full website page whereas fragment as a part of that website page. So, activitycan contain any number of fragments.
I think :
1.In the changing fragment you just change part of activity and not whole of them , but by intent you change whole of activity to other.
2.by intent you can communicate between android components such as activities, content providers, broadcast receivers and services, but by fragment you cant and in the otherwise fragment is child of activity.
I'm developing a simple home launcher. Main activity is the home layout, in it there is a button to call another activity to show the apps list. In this second activity, if you long press an icon, it adds to home screen.
So, from the second activity (apps list) I add an imageview to the main activity (home screen), but to do this I need to get the main layout, which it's not possible from different activity than itself.
To achieve this, I'm thinking different options like this:
1-Declaring a variable in the second activity:
public static RelativeLayout mainLayout;
Set it in the first:
// MainActivity
Intent i = new Intent(this, DrawerActivity.class);
DrawerActivity.mainLayout = (RelativeLayout)findViewById(R.id.mainlayout);
startActivity(i);
2-Using fragments avoiding the jumps between activities.
3-Changing the layout in the main activity (one activity for two layouts).
4-Declaring class descending from Application and store here everything I need in the different activities.
5-Use of broadcasters.
The question is: which is the right approach to achieve this ?
I've read several docs but there is no clear answer.
Definitely don't go with option 1. That's the best way to leak memory and context, and you don't want that. Resources associated with an Activity (in this case the layout) should be private to that Activity. This ensures that the framework can manage memory as it was designed to.
To communicate between activities, the official way is to pass parameters in the intent that launches the activity. You can add the identifier of the application you want to add to the main screen, and than retrieve the image either from the intent (as seen in the link) or if second Activity is launched for a result, then in the onActivityResult method of your home Activity.
However in you case I would suggest another approach. As you have to persist the layout of the home screen, I would create a database table containing the positions of the applications added to the main screen. I would modify the database entries where it's convenient and rebuild the main screen's layout every time it is displayed based on the database.
Is there any way to change the parent of an activity from class file.
I have two activities say ONE and TWO, and both of them can start a new activity say THREE.
Now if ONE starts THREE, then for up Navigation parent should be ONE.
But if TWO starts THREE, then for up Navigation parent should be TWO.
If i can change the parent for an activity from my class file.
I will send some data from activities (ONE and TWO) to uniquely identify them then set the parent according to that data.
If there is any other way to do this then let me know.
I think you can only have one defined parent in your manifest but you could just send your parent when you launch your activity. Just pass a bundle with what activity launched your Third activity and handle it in OnBackPressed and if you want to change your up button handle it in onOptionsItemSelected.
In android activities are added in stack as they are started. Having said that, when you start activity THREE from activity TWO, activity TWO is added in stack below THREE. Now on click of up button in activity THREE, you can finish activity THREE and the control will automatically return to activity TWO.
Please follow this post for getting up button press in android. Now in its onClick case, you just need to finish activity THREE, and the control will return to whichever activity you came from. Whether it be ONE or TWO. You can finish the activity in onClick as following:-
Just write
finish();
in the case for home button
I am using tab widget and each tab belong to a activity group. An activity group has several sub activities.The sub activites are added to the activity group via LocalActivityManager.startActivitY from activity group but now i want to use startActivityForResult from instead of startActivity. Can someone help me to accomplish that as LocalActivityManager class does not havestartActivityForResult method..
#Sabya i had been there in the same situation few weeks past. And i have found from my side that there is no way to use startActivityForResult in ActivityGroup. So i found some hacking type of technique to get out of this. First create a static variable of the result you want from startActivityforResult.After doing this,intent to a transparent Activity,in which you doing all those stuff of activityforResult.After getting value you set the value of the static variable that you created in the TabClass,After then finish the transparent Activity
in my app i am having an Activity that launches another Activity ontop of it. The second Activity is meant to provide controls like next and previous for the first activity.
How can i pass button events between the two activities that are visible at the same time without closing the Activity with the controls?
You can send Broadcast in order to communicate between Activities.
In parent Activity register BroadcastReceiver, then send a Broadcast by calling sendBroadcast(..) in child Activity.
It sounds like what you are trying to do doesn't require two activies, but rather additional view objects on the main activity that will be shown/hidden/removed/added as buttons on this activity are pressed.
If you need additional help, I would recommend elaborating on your original post.