How to pass string between two activities - android

I have two activities A and B, and from activity A, I click on a button that opens a dialog box which contains a form consisting of two edit text fields and a button(the button in the dialog box is used to start activity B). So, my question is: how do I pass a string from activity B to activity A, but without closing the dialog box(the string will be used to fill one of the two edit text fields).

You need to create a class to store the variable. In ActivityB use set the value of the variable, the created class stores it and in ActivityA get the value of the variable.
Create a class: GlobalVars.java. In this class put this:
public class GlobalVars extends Application {
private static String var2;
public static String getVar() {
return var2;
}
public static void setVar(String var) {
var2 = var;
}
}
In ActivityB put this line in to the appropriate place:
String something;
GlobalVars.setVar(something);
In ActivityA put this line in to the appropriate place:
String getsomething = GlobalVars.getVar();
And that's it!

It sound like you want to keep dialogbox when activity B returns result. If such case then you can open dialog box onActivityResult:
Activity A
Click on button open dialogbox
start Activity B
return result to Activity A
onActivityResult will call
open dialog box again
Note: Activity A must not be SingleTask, SingleInstance, SingleTop.

Perhaps try using sharedpreferences !?

You can use the broadcast system to send an Intent containing data to another activity.
Search google or stackoverflow there are a lot of tutorials and examples of how to achieve this.
as i understand you want activity a to get notified and fill a field based on some action in the dialog.
what i am suggesting is one way of doing this. the other answers provide also different solutions to the same problem. also you can register an interface with the creation of your dialog which will be called from inside the dialog and do something in the first activity.

I think you need to use Bundle and static global variable and onActivityResult(). If you want to edit client with previous client to new client . Suppose you have "ClientList" Activity and "EditClient" Activity
Write into "EditClient" Activity
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String name = extras.getString(ClientList.KEY_Client);//ClientList.KEY_Client is global static variable of "ClientList" Activity.
if (name != null)
{
nameText.setText(name);//"nameText" is a EditText object represent EditText view
}
}

Related

How do I display the content of activity2 in a dialog Box that is called in activity 1?

I basically have an activity Which has several layouts for different contents. For example there is a switch statement that is responsible for switching between different content views. Each view contains a parameter which I determine through selecting a radio button. This choice is then retreived in my onActivityResult of my previous activity.
I want to do the same thing but rather going back and forth between these two activities I want the one with the parameter selections to display its content in a dialog box. So in other words when I click on something in Activity one a dialog box pops up with the content of one of the views of Activity 2.
How it was:
Activity 1------>Activity2
Activity2 data ---stored---> Activity 1
what I want now:
Activity 1 ---open dialog box----> dialog box contains Activity 2's parameter selection
I make my choices and when the dialog box closes the data is stored in Activity 1.
Is this possible?
If so how? If not what is another approach I could use?
You can achieve this using LocalBroadcastManager
Code in Activity 1 in oncreate() method:
LocalBroadcastManager.getInstance(this).registerReceiver(tempReceiver, new IntentFilter("dialogData"));
Outside of onCreate() method of Activity1
private BroadcastReceiver tempReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
/* show dialog and
you can get values same like bewlow code*/
if(bundle!=null) {
String dialogData;
try {
dialogData= bundle.getString("your_key");
}catch (Exception e){
e.printStackTrace();
}
}
}
};
Code in Activity2 under onBackpress() method:
Intent intent = new Intent("dialogData");
intent.putExtra("your_key", "yourVaue");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
finish();
In this code yourValue is value of selected views

How can i make a relation between an image button and a function or activity?

I must write a program with android which can find ssid and show it. My problem is how can i make a relation between an image button in first page and an activity or function in other page.
Buy the way i'm beginner and download the code of searching method because of that i can not recognize which one is the main method or function for pass it to setonclick method that i write for image button in first page?
please answer as soon as you can i need it immediately.
If you are trying to call another activity's method from your existing activity then you should use these steps.
First save the context or instance of the callee activity; for this you can use a class that holds your global data. In this class make the object of this activity.
FirstActivity first = null;
When the callee activity is first created, initialize this instance.
public void setFirstActivity(FirstActivity factivity)
{
first = factivity;
}
And when you do need to call this callee activity's method then access this instance from this global class and with the help of this instance, you will have access to the methods of the callee activity.
public FirstActivity getFirstActivity() {
return first ;
}
use it to get access to the methods or instances of the activity.
Click on the ImageButton, and add the name of the function in the On Click property (Simply the name, like myFunction)
In your code, copy-paste this function and replacemyFunction by the name of the function you chosed
public void myFunction(View v)
{
String ssid = ((EditText)findViewById(R.id.EditTextID)).getText().ToString();
Intent intent = new Intent(this, SecondView.class);
intent.putExtra("SSID_KEY", ssid);
startActivity(intent);
}
And replace SecondView by the class that displays your second page and EditTextName by the ID of your EditText.
In the OnCreate function of your second class, you can get your ssid using the following code
#Override
protected void onCreate(Bundle savedInstanceState) {
String ssid = getIntent().getStringExtra("SSID_KEY");
//Do other work here.
}
Also, on top of the class containing the ImageView, don't forget to add these lines:
import android.view.View;
import android.widget.EditText;

Pass reference of one Activity to another Activity

I know that I can pass some values between Activities using intent.
However, if I want to pass whole Activity to another Activity I think it is not good approach.
Is there another way to do that?
I have Settings Activity in which I am changing some Colors. So after I come back to my Main Activity I would like to apply those colors. To do this, I need access to MainActivity fields after I change Color value, so inside PreferenceActivity. In other words, I want to have access to Main activity fields from PreferenceActivity class. Any ideas?
You should be using a SharedPreference and then accessing that in your main activity. I recommend you reading up at http://developer.android.com/guide/topics/ui/settings.html because it seems like you are implementing your settings activity incorrectly. The part you might be specifically interested in is the "Read Preferences" section. However, I strongly suggest you read through the whole thing and then implement your settings the proper way.
Updated answer with the 3 different ways (that I can think of):
1) Start your preference activity using startActivityForResult(), then in your onActivityResult() access the SharedPreference and make your necessary changes. See here
2) Register a SharedPreferenceChangeListener with your MainActivity, which will be called when any changes happen to your SharedPreference. See here for a detailed discussion. Also see my initial response.
3) In your MainActivity's onResume(), access the SharedPreference and then make your changes there. I do not like this method because you will be cluttering onResume() with more logic and you will also probably have to have a variable that keeps track of the state of the variable you are interested in.
I would personally go with option 2 because the callback was created for this exact purpose.
I think you could pass the value by using method putExtra(name, value).
And after you start new activity you can get the value you pass before by using method getStringExtra(name).
Shared preferences can be used. If you want your changes to be reflected right away add listener. Refer to SharedPreferences.onSharedPreferenceChangeListener. Its an easy way to do.
If you want to lots of changes required in many activity from you change in any one.
And access last modify data from all Activity and modify also.
for example.
Constants.java
public class Constants
{
public static String name;
}
In your MainActivity you have an editText.
MainActivity.java
public class MainActivity extends Activity {
private EditText yourName;
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourName = (EditText) findViewById(R.id.yourName);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Constants.name = yourname.getText().toString();
Intent intent = new Intent(getApplicationContext(),Activity2.class);
startActivity(intent);
}
});
}
In your Activity2 you have an TextView and that getting value which you enter in MainActivity.java without pass in Intent.
Activity2.java
public class Activity2 extends Activity {
private TextView yourName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourName = (TextView) findViewById(R.id.tv_yourName);
// directly use ferom serializable class
yourname.setText(Constants.name);
}
like that you use many values from all activity and modify from all activity.

Android: problem in getText method of EditText

In my project I have two activities or classes. In first activity I have a EditText and I want to get the text of it from second class.
In the first class I wrote this code but it seems has problem.
public String getTextMessage()
{
return textMessage.getText().toString();
}
because in second class when I want to get it, program crashes.
message = encode.getTextMessage();
What is your suggestion?
As told by sunil you have to firstly get string from edittextbox and through intent send it to another second activity. After start of second activity you have to get text from bundle.
code snippet is given below...
Activity A
Intent i = new Intent(this, Second.class);
i.putExtra("EXTRATEXT", editText.gettext().toString());
startActivity(i);
Activity B
Class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String text = getIntent().getExtras().getString("EXTRATEXT");
}
Access the text by getText() from edit text and store it in an string. when you move to second activity sent string variable to second class via bundel. Extract the bundel in second class and use it.
You have to pass the value through intents

Sharing data between tabs

I am getting some glitches while making a tab enabled application.
I want to share data, between two tabs of my application.
How can I achieve the same?
Rgds
Robert
the correct way is setting a static field into the activity that creates the tabs
public class greformanews extends TabActivity {
public static String JorgesysTitle;
...
...
...
so in your Activity defined in tab 1
#Override
protected void onPause() {
greformanews.JorgesysTitle = "JORGESYS =)";
super.onPause();
}
in your Activity defined in tab 2
//get value defined in Activity 1 !:)
String Title = greformanews.JorgesysTitle
You need to use intents to different activities, or in that case of tabs.
Go to Android Common Tasks
And look at the subject below "some intent examples". This will get you started.
You basically need to put whatever values you want into a bundle and pass that over to the new activity using intent.putextras();

Categories

Resources