This question already has answers here:
Sending data back to the Main Activity in Android
(13 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
i have two class, MainActivity and InputAddress, how to load url from class InputAddress
view.loadUrl("here, im confused");
send data from inputAddrss,
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("url", YOUR_EDIT_TEXT.getText().toString());
startActivity(intent);
receive data in MainActivity,
String s = getIntent().getStringExtra("url");
then load into webview
view.loadUrl(s);
You can add a public static variable like this:
public class InputAddress {
public static String address = "abc";
}
then you can access the address variable in MainActivity like this:
public class MainActivity {
// ...
public void onCreate(....) {
Log.d("TAG", InputAddress.address);
}
}
Hi Deki Kurnia Hadi Permana,
There are lots of way to transfer data from one class to another class, But for the activity no need to do anything they provide functionality to transfer data one to another activity using "Intent" data,
Following is the code where you can send and access data in another activity.
FirstActivity.class
Intent callIntent=new Intent(FirstActivity.this,SecondActivity.class);
callIntent.putExtra("urlToLaunch","post url here");
startActivity(callIntent);
SecondActivity.class
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String urlToLaunch=bundle.getString("urlToLaunch");
// set In webbrowser
}
Related
I'm trying to send HashMap to start new activity with startActivity(intent).
I followed the answer from this link:
How to send hashmap value to another activity using an intent
But I still get error
Of course, i tried to send String
intent.putExtra("test","some String");
startActivity(intent);
And it worked
My code:
HashMap<String,Diner> dinersOrdersHasMap = new HashMap<String,Diner>();
FillHashMap(); // Fill the HashMap with data
Intent intent = new Intent(this, BillForm.class);
intent.putExtra("dinersOrderHashMap",dinersOrdersHasMap);
startActivity(intent);
The exception:
"Could not execute method for android:onClick", e);
e.detailMessage = "Parcelable encountered IOException writing serializable
object (name = com.example.myfirstapp.Diner)
When i call
startActivity(intent);
is your Diner class Serializable or Parcelable ?
Could you post here your Diner class ?
I have a activity where you select a number in a spinner(dropdown-list in AndroidStudio) and sends it to a new activity/another class, before it is sent to a server. The array-adapter works fine, but using the getExtra Intent in the receiving activity is a lot of trouble for me.
The app crashes and logcat give this message:
NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference.
MainActivity.(MainActivity.java:76)
Line 76 in MainActivity String avd_nr= getIntent().getStringExtra("getData");
This is my code for passing the array value, and the line Log.i("data",avd); posts the spinner value(avd) in the logcat.
btnAvdeling.setOnClickListener(new View.OnClickListener()
{
final String avd = dropdown.getSelectedItem().toString();
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("getData",avd.toString());
startActivity(i);
Log.i("data",avd);
}
}
This is my code where I receive data
String avd_nr= getIntent().getStringExtra("getData");
private SurveyResponse fillInResponsData(Integer answer) {
surveyResponse.setAvdeling(avd_nr);
return surveyResponse;
}
Please give some help on what i need to add or change
in onCreate() Method put the following :
String avd_nr =getIntent.getExtras("getData");
String avd_nr =getIntent.getExtras("getData");
Its should be on onCreate() method
Hi everyone i am having a problem here so here is what i am trying to achieve.
Achieve:
Trying to create a Map and share it between activitys! And use it in MainActivity.
Reason:
Trying to avoid the big load of the data on the MainThread by loading the data in my SplashActivity into a Map and sending the loaded data into my MainActivty
so i can use it there to check it from user Input if it matches.
Problem:
Problem is that the data i am trying to load is this one:
http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt
Which is a big wordlist and it takes a while to load in background. It has to be fast thats why i am trying to load it in Map before the MainActivity Load and then passing it when its done loading btw user is waiting for SplashScreen while load to the Map.
Getting error that the data i am trying to send true Bundle, Intent is too large.
Anyone having thoughs how i can pass the loaded Map into the MainActivity?
Yes i know i can use AsyncTask as the background thread...
Yes i know it can be done with Serialize but please show some examples of your thoughts.
Thanks for everyone who is trying to help!
ThePreviewActivity code:
public class PreviewActivity extends AppCompatActivity {
private Scanner scanner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Map<Character, ArrayList<String>> charCount = new HashMap<Character, ArrayList<String>>();
//get Assets
AssetManager am = this.getAssets();
//create input stream
InputStream is = null;
try {
//open the word txt
is = am.open("words.txt");
} catch (IOException e) {
e.printStackTrace();
}
if (is != null) {
scanner = new Scanner(is);
}
while (scanner.hasNext()) {
char firstChar = scanner.next().charAt(0);
ArrayList<String> list;
if (charCount.containsKey(firstChar)) {
list = charCount.get(firstChar);
} else {
list = new ArrayList<>();
}
list.add(scanner.next());
charCount.put(firstChar, list);
}
Intent intent = new Intent(this, MainActivity.class);
Bundle extras = new Bundle();
extras.putSerializable("HashMap", (Serializable) charCount);
intent.putExtras(extras);
startActivity(intent);
finish();
}
}
MainActivity.java code:
Bundle bundle = this.getIntent().getExtras();
if(bundle!=null) {
charCount = (Map<Character, ArrayList<String>>) bundle.getSerializable("HashMap");
}
Error i am getting trying to pass it with a Bundle: Caused by: android.os.TransactionTooLargeException: data parcel size 3560868 bytes
First, please move all this I/O-and-parsing code to a background thread.
In terms of your TransactionTooLargeException, you simply cannot pass that much data around via Intent objects using startActivity(). You could:
Store this data in a singleton, particularly since it will not be changing (presumably), or
Wrap the data in a ContentProvider, and have other activities use a ContentResolver to work with that provider
I am fairly new to java / android programming and have mostly been following tutorials / reading. I have 2 classes called MainActivity and Homeactivity, the MainActivity is where the user logs in and HomePageactivity is opened via intent if the login is correct.
The username is passed through an edit text to which i have used the following code in the main class
String CurrentUser = editTextusername.getText().toString();
public String GetCurrentUser ()
{
return CurrentUser;
}
And this in the homepage class
MainActivity testing = new MainActivity();
String x = testing.GetCurrentUser();
CurrentUserName.setText(x);
This seems like it should work to me, how ever when launching my application it just crashes, and without the lines of code in the main activity it works fine
Any ideas as to what im doing wrong here guys
The String may be null during setting to TextView check before setting to textView
String CurrentUser = editTextusername.getText().toString();
public String GetCurrentUser ()
{
if(currentUser.length>0&¤tUser!="")
return CurrentUser;
else return "empty";
}
String x = testing.GetCurrentUser();
CurrentUserName.setText(""+x);
You can use intent to carry info from one Activity to another.
Read this: Intents
It is quite easy to transfer strings and primitive data types via intent, using putExtra() .
Some example code to make it more clear:
//this runs, for example, after a button click
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("username", userName);
startActivity(intent);
At the other end you can take them as like this:
String username = getIntent().getStringExtra("username");
I'm having a problem with an android application that I'm working on.
My application has several sections and the next screen that loads is based on a string. So, screen 1 of section 1 would be, S1S1.
My question is, how can I start an activity based on a string. I have S1S1 saved in a string, let us call it next activity. Rather than having to type S1S1.class, I need it to come from the string. I've tried everything I can think of and google hasn't helped much.
Some things I've tried are
Intent myIntent = new Intent(nextactivity);
Intent myIntent = new Intent(v.getContext(), getClass().getName().valueOf(nextactivity));
Intent myIntent = new Intent(v.getContext(), Class.forName(nextactivity));
and tried running with
startActivityForResult(myIntent, 0);
but nothing seems to work. Any ideas?
Here is a code by which you can start activity using the name of the activity
String activityToStart = "com.example.MainActivity";
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException ignored) {
}
EDIT
Here class name will be full name of the class with the package name.
For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.
An even better way (and one that is used in the system to launch Browser.apk along with other apps that aren't bundled with AOSP):
Intent intent = new Intent();
intent.setClassName("com.android.browser","com.android.BrowserActivity");
context.startActivity(intent);
Alternatively, if you want to check that you can start the Activity from the command line, you can do something like this from your shell:
adb shell
am start com.android.browser/.BrowserActivity
I am not aware of solution but i have an alternative.. the way similar to div hide and show in web pages.
if your s1s1 is to loaded low content have them in a linearlayout and keep their visibility gone on loading form s1. when you click on s1 to reach s1s1 hide s1 and set the params of visibility to "visible".
By doing this you can avoid creating a separate activity and this way is also easy to navigate back.
Use Enums!
public enum SectionActivity {
S1S1(MyS1Activity.class),
S1S2(S2Activity.class);
private Class<? extends Activity> activityClass;
private SectionActivity(Class<? extends Activity> clazz) {
this.activityClass = clazz;
}
public Class<? extends Activity> getActivity {
return activityClass;
}
}
Then somewhere in your code:
SectionActivity act = SectionActivity.valueOf(string);
Intent intent = new Intent(this, act.getActivity());
startActivity(intent);