I have 3 activities and want it to be possible to fill in a form on the activity after a 1 button takes me to the second activity that calls for his third activity. In the third activity I choose a value and want to go directly to the 1st activity but want the 1st activity is in the state it was in when it comes out.
I have this in 3th activity:
Intent selectFavorite = new Intent(view.getContext(), FirstsActivty.class);
selectFavorite.putExtra("Data", Info);
selectFavorite.putExtra("SUB", favoriteListArray.getJSONObject(position).getString("sub"));
selectFavorite.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
When i return the fill is blank :s
I am 99% there is a different and more reliable way to accomplish what you desire. If by any chance the reason you are going to SecondActivity and ThirdActivity is to get some input values for the FirstActivity's form, then you should consider doing this not in a different Activity, but using dialogs or similar. That way you will avoid having to unnecessarily manage more activity's lifecycle overridden method than what you actually need.
If you agree with me and you don't know if you should use dialogs, or action bar menu items or similar for what you want to accomplish, try checking out the Android design guidelines: http://developer.android.com/design/building-blocks/dialogs.html
Related
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.
I'm a very new to Java. I thought I was doing okay but have now hit a brick wall, so to speak.
The idea is I use the 'home button' '_menu' for the user to choose one of 26 formats. The format they choose has 3 variables. Those 3 variables are then used in the first xml view, along with 2 user inputs to calulate another variable. This variable is then used in a second xml view, along with a third user input here, to calculate the final answer.
I have created the 26 choices and if for example I choose option 5, on the emulator screen I see all the correct values associated with this choice. I don't think those values are getting stored anywhere. I say this because if I come out of that view and return back into it, it's not showing, as in my example, choice 5. It's showing its initial state, as though I was going into it the first time. I assume it's something to do with launching this activity from the start but is there anyway around this. Or really where do I start.
My second question is with the integer variables that I created from this choice. I need to pass them into another java file for the first set of calculations. I've tried to pass the variables/data with the, 'new intent putExtra' but can't get it to work. But I don't think I can use this anyway since the I don't want to launch the second view directly from the res/menu/ .xml view.
Not sure if this is making sense to anyone. Can someone help point me in the right direction?
I don't quite understand your first question, but it sounds like you're launching a new activity and when you quit and come back to it, everything is reset. If you're launching a new activity after selecting the options from your phone's menu button, you should implement a method that saves data to the shared preferences of the main activity. This method should be called on the activities onPause(), onDestroyed(), or onStop() method. You can also add a method on onResume() where the activity checks if there's any data stored in shared preferences and if so, make the desired changes.
As for your second question...I kinda don't understand it either. new intent and putextra is used when you're starting a new activity and want to pass data to it. Views are not "launched" they are only inflated and brought on display whenever you want. I did an app once where I had everything in one activity and just using setContentView() method all the time. In the long run, it just complicated everything. It is easier to keep things simple and launch activities. Here is an example of some variables being passed to a new activity:
On my main activity (FirstActivity) I have:
(when newActivityButton is clicked)
case R.id.newActivityButton:
Intent mIntent = new Intent(FirstActivity.this,SecondActivity.class);
String[] luckyNumbers = {
luckyNumber[0].getText().toString(),
luckyNumber[1].getText().toString(),
luckyNumber[2].getText().toString(),
luckyNumber[3].getText().toString(),
luckyNumber[4].getText().toString(),
luckyNumber[5].getText().toString()};
mIntent.putExtra("luckyNumbers", luckyNumbers);
mIntent.putExtra("message", messageField.getText().toString());
FirstActivity.this.startActivity(mIntent);
break;
luckyNumbers[] is an array of textviews.
Then on my NewActivity onCreate(), I have:
message = getIntent().getExtras().getString("message");
Log.i("TAG", message);
luckyNumbers = getIntent().getExtras().getStringArray("luckyNumbers");
cv.setLuckyNumbers(this.luckyNumbers);
cv.setMessage(this.message);
where cv is just a custom view I created with its own methods
In an app I have very reused Activity, that shows a list of stuff happening on a specific day. The day is specified using Intent Extras.
My problem is, that if the user starts at day=1, then chooses day=2 and then day=1, from the menu, then I would like the back button to go to day=2 and then home. That is, I want to do REORDER_TO_FRONT, but not just based on the name of the activity, but also its extras.
There doesn't seam to be any intent flags suitable for this purpose. I've considered implementing my own 'sub activity stack' using onNewIntent, but it probably wouldn't work very well.
Have you tackled similar problems in your apps? Is there perhaps a way to programmatically access the activity stack, and choose which one is suitable to return to?
Manage your own Activity stack! If I'm not mistaken, you use the same Activity to display each day. Make it single top (FLAG_ACTIVITY_SINGLE_TOP). In the launching intent, pass on the current stack, in your example "121".
Respond to the back button event by launching your Activity with a smaller stack: "12" - or if stack is already "", then just let the Activity handle Back event. Then as you mentioned, use the onNewIntent function to update your Activity.
How do you handle multiple screens in an android application? I have developed with the tab bar at the bottom without problem, however what I am wanting to do is replace all the content on the screen with the content from a new .xml layout file I have created in the project. Additionally, how would I tie the back end code to the new layout file? I'm sure this question probably exists already and is googleable (may have made up a new word). However, I don't know exactly what it is that I am looking for. Thanks in advance for your help.
What you need to do is, create a new Activity and add it to the AndroidManifest.xml:
<activity android:name="ActivityClassName" android:label="Label for the Activity"></activity>
and can be called in a method:
public void startActivity() {
Intent someName = new Intent(CurrentClass.this, ActivityClassName.class);
startActivity(someName);
}
Android applications generally use a separate Activity for each screen, and switch between them using Activity.startActivity and Activity.startActivityForResult. You can pass arbitrary data to an Activity via Intent.putExtra.
Hope this helps,
Phil Lello
It really depends on how you want your application to flow.
Let's consider the scenario where a user does the following:
Starts your first activity
Presses the 2nd tab
Presses the 3rd tab
Presses the back button
If you use a separate activity for each screen, then the following would happen
Activity 1 is started
Activity 2 is started
Activity 3 is started
Activity 3 is closed, user returns to Activity 2
(in this case pressing the back button again would you take you back to Activity 1, and pressing it again would exit your application)
If you used one activity for all the tabs, then the following would occur
Activity 1 is started
Activity 1 sets tab content to tab 2 content
Activity 1 sets tab content to tab 3 content
Activity 1 is closed, user returns to home screen
If you are using a screen with tabs, then the second method (a single Activity with a TabHost or similar) is the preferred method, otherwise the user will end up making a large activity-stack just switching between tabs (meaning if they switch between tabs a lot they'll have to press the back button a lot of times to exit).
If you want to go for the single activity approach, then do some research on TabHost and TabContentFactory. In the createTabContent method of your factory you can inflate a View/layout from XML to set as the tab content using View.inflate. Look those up and come back ask another question if you get stuck ;)
i think you may want to play with more than one activity.... you can have multiple activities and one xml for each of them... in this way you can have different screens... check these links. Multiple Activities, Creating an Activity.... hope this helps...
I have a question on the Android Activity, for example I have a TabHost, and there are included four Activities, the first tab is a search Activity, enter a keyword in the current result of this Activity to return, and in the current Activity display. Is called to display the search results themselves. And after searching several times, and then return to key mobile phone keypad, the display is the result of the last search keyword, I want the press back key to return to the last call of the Activity or TabHost. Should I do?
By the way, in a tab in the use of Intent calls a Activity,
eg: host.addTab (host.newTabSpec ("friend"). setIndicator ("search")
. SetContent (new Intent (this, Search.class)));
In this Activity in the need to call another Activity,
e.g: startActivity (new Intent (this, Other.class));
Also called another Activity displayed on this tab, but not yet jump out of the show. I ask how you can achieve this?
First, you can use startActivityForResult(...) instead of startActivity(...). This means that when you're done with the activity, you want to come back to the activity that started it so you can do more.
Second, you can override the onKeyDown(...) method and define whatever behavior you want for the back key. however, this is not recommended by Google except when absolutely necessary.