Can I send a View to another Activity? - android

my question is: If for example I have a RelativeLayout in Activity1, can I send that RelativeLayout to Activity2?
In my app I have 3 different RelativeLayout in Activity1, and if the user Clicked on one of those RelativeLayout I would to send it to my next activity. Any ideas on how to do that?

From your comment it appears you need to load a different layout based on user's selection from previous screen. This problem can be tackled in several ways. One of them is to pass the user preference from Activity1 to Activity2. To do this, in Activity1:
int choice = 0; // assuming user made choice 0
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("choice", choice);
startActivity(choice);
Now, you can read the second value in Activity2 and accordingly load your layout (i.e. different view) as (within onCreate()):
int choice = getIntent().getIntExtra("choice", 0); // default choice 0
switch(choice) {
case 0:
setContentView(R.layout.layout0);
break;
case 1:
setContentView(R.layout.layout1);
break;
default:
setContentView(R.layout.layout2);
}
Thus, based on user's choice you can choose a different layout and write you business logic accordingly thereafter.
Another option you have for your use case is that you define different activities for each of your layouts and then call startActivity() to appropriate activity. This will separate out the business logic of handling the different layouts and may therefore be easy to maintain.
You can also go the fragment way but, ultimately it would also employ one of the above methods. Note that the method you choose highly depends on your use-case and therefore I cannot make that decision for you.

You can't do that since each Activity has its own layout.
What you can do instead is send the data needed to setup your second Activity's RelativeLayout so that it looks like the one in your first Activity via Intent extras.

I'll just give you an idea , you can send data from the first activity to the second using Intents , however you can create a layout dynamically using data received by the second Activity and adding all the parameters you need.
I hope it will help you

Related

Best approach for accessing activities from another

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.

Back 3 activities and fields not blank android

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

How to set the contentview of an activity using a button of another activity

I have a button on the main activity that when clicked runs a method. In the method I have an intent to a second activity, but i want to set the content view of that activity with the button from the first activity because i want to have more than one button on the first activity, but reuse the second activity and just change the layout. So....
Click button1 > sets view to layout1 > starts activity with layout1 as the contentview
OR
Click button2 > sets view to layout2 > starts activity with layout2 as the contentview
I would like to do it this way to avoid creating too many activities
Thanks
there are many ways to do it. here are just a few of them:
pass the argument of which layout to use by adding extra int to the intent (putExtra) and on the onCreate of the second activity use the intent to get it.
use fragments instead of activities , there you would have even more ways to do it.
not recommended - using a static int.
Add extra info to your intent using Intent.putExtra(String key, int data). Then in the second activity use getIntent().getIntExtra(key). You can use something other than an integer for your data but I recommend it so you can easily use a switch block.
EDIT:
Also, as yarian said:
You can just pass the layout itself, it's just an int that sits in the R file.
It is probably a good idea to do this to eliminate the switch block (unless you need to execute other code as well, but this is still a good idea as you won't be defining separate constants for each layout to pass) So in your first activity say:
intent.putExtra("LAYOUT", R.layout.layout_name);
And in the second:
setContentView(getIntent().getIntExtra("LAYOUT"), DEFAULTVALUE);
Hope I helped!
When either of the buttons is clicked you start the second activity through an intent, in which you put value 1 if the first button is clicked, value 2 if the second one is clicked.
Then in the second activity you read a value from an Intent and if it's 1 you setContentView to be the first layout, if it's 2 to be the second layout.

Passing the user selection value from one activity to another

I am making a small calculation game.
on the main screen there would be a RadioGroup
o Easy o Medium o difficult
Button Continue
then after the user selects this and presses continue then on next page another radio group
o Addition o Subtraction
Button Start
Now I want to display questions depending on the selection of radio buttons from there 2 activities.
How can I do that, for a single radio group I can use changeListener...But here I have to consider values from 2 radio groups simultaneously.
So how to do that. I tried my best to explain this using example and representation too.
Thanks
Use a bundle with Intent extras to pass data between activities.
Intent i = new Intent(getContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putExtra("key", value);
i.putExtras(b);
startActivity(i);
http://developer.android.com/reference/android/content/Intent.html#putExtras(android.os.Bundle)
and in your SecondActivity
getIntent().getStringExtra("key");
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
For the apps that I've been involved with I've been using a application object to store state variables that need to be visible in more than one activity. The application object gets created before any activity objects, and is the last thing to be destroyed when the application is closed, so it's a good place to store state variables. You're allowed one application object per app, and the class type needs to be declared in the manifest. It sounds like your difficulty variable and your add/subtract are state variables, so I'd suggest you go down this path.
You may also want to use the SharedPreferences if you want to save the value longer the current instance.
See for more details.
http://developer.android.com/guide/topics/data/data-storage.html#pref

Using Primitive Data Types in another Class and the res/menu/.xml file

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

Categories

Resources