This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 5 years ago.
I've been working in infoSec for almost a decade and now I'm trying to pivot into Android development. I'm completely new in the field of Android. This question has probably been answered before, but I didn't find anyone using pictures. So here we go:
I want to grasp this thing with Fragments and activities.
Activities and fragments image
Basically I want to do a simple thing; get the result from GameFragment, pass it to GameActivity, pass it to ResultActivity, and then show it in the result fragment. (Or if there is another way to communicate with fragments in different activities)
Yes there is many way below i mention :
you can use shared preferences.
you can use sqlite.
you can pass data using intent.
Links will provide brief knowledge about this topics.
You can access the parent Activity of a Fragment by using getActivity() method. To pass data between activities you can use Intent s.
The simplest solution would be use of static variable in any common Class or in secondActivity.
If you use in second receive that value using
variable = getActivity().getIntent().getExtras().getInt("key");
Then use "variable" value.
Or declare in a common Class as like
public static int variable;
Assign value
CommonClassName.variable = 1;
Then use it.
Related
I'm a beginner Android developer. I'm still learning new things every day, and while learning I have heard this question quite frequently: What will happen if we send View object as parameter within OnCreate Method? I have searched about it but didn't find anything helpful. I just want to know is it really possible and if so then please explain the scenario.
oncreate() called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state.
I declared String A in Class A and I want to send this String A to Class B(MainActivity that include inputView) for appending to inputview.
I found,
Changing text from another activity
This solution is very simple and easy.
However, android studio warned me that
"Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)".
Any better solution?
Intent is the proper way to achieve data communication over Activities
What's the best way to share data between activities?
First of all, please note I´ve been trying to search and existing answer to this question. I found this question, but is not helping.
I am simply trying to get strings from regular Java (domain) classes within an Android app, but I am not able to get them by using getResources().
Is this possible? (I´d like to avoid creating a new strings.xml alike file in order to centralize all the strings within the app)
Thanks in advance.
EDIT: Am I forced to pass Context to every single Java class? Is there any way to avoiding it?
Pass context to the Java classes in their constructors. Assign this context to class variable.
Then you can do mContext.getResources().getString(---)
You will have to also import the R.java in this class
You need to have access to a Context object (passing it so a non android class e.g.).
Using this Context you can call context.getResources().getString(R.string.mystring);
This question already has answers here:
Android global variable
(14 answers)
Closed 8 years ago.
In my android application I need a place for putting the variable member id .
The problem is , it is getting from the online API and I need to find a way to store / retrieve it
I have tried to put it in a custom class , but the problem is , it will lose if I kill the activity, I have also know that there is a way to extends the application.
So I would like to know what is the best way to store global variable?
I have to implment:
Save the variable on onSaveState
Save it on sharepref
Save it manually
Retrieve it manually
Thanks
Update:
Thanks for reply. If I have just 3 variable (simple data e.g. a boolean , a phrase ), and I need it after app restart , should I simply use share pref to store it? What is the drawback of it? e.g. Will it harmful to the performance? thanks
You can create the global variable in android by declaring them in the class which extend the Application class.
Something like this.
class MyAppApplication extends Application {
private String mGlobalVarValue;
public String getGlobalVarValue() {
return mGlobalVarValue;
}
public void setGlobalVarValue(String str) {
mGlobalVarValue = str;
}
}
MainActivity.java
class MainActivity extends Activity {
#Override
public void onCreate(Bundle b){
...
MyAppApplication mApp = ((MyAppApplication)getApplicationContext());
String globalVarValue = mApp.getGlobalVarValue();
...
}
}
Update
This hold the value until your application is not destroyed. If you want to keep your values save even after your application instance destroy
then you can use the SharedPreferences best way to do this.
Learn about the SharedPrefernces from here : http://developer.android.com/reference/android/content/SharedPreferences.html
If you need a global variable, which is shared between multiple activities and survives their lifecycle changes, but is otherwise not persisted and would not survive an application restart, the method suggested in the first answer (extending Application and putting it there) is ok.
If you need a persisted global setting, which should also survive an application restart, then you should use one of the other methods suggested. SharedPreferences is probably the easiest to use.
See also this Stack Overflow question: What's the best way to share data between activities?
You can store it in a SharedPreferences, and create a sharedpreferenceHelper class to retrieve/store it.
Its always better to store values which you want to use multiple times in one of the following ways:-
Shared Preferences
Singleton classes
Write it to a file
SQLite Database
Store on the web-server
This question already has answers here:
How to declare global variables in Android?
(17 answers)
Closed 2 years ago.
I have an application that uses a custom class. When the app is started I populate an instance of this class in the main activity with all the data it will hold.
Basic data is then shown in a ListView from which you can select a ListView item to see further information displayed in another Activity.
Currently, I am having to pass the data that is relevant to the new Activity to it by using:
intent.putExtra("NAME", value);
I want to implement a ViewPager so the user can easily switch between ListView items. Therefore the currently use method isn't very good as I only have the data for one entry at a time and would need to get back to the original Activity to get all the data again.
Is there a way to have my class objects globally available ANYWHERE in my application? I feel my applications code is getting bloated as I am overcoming these issues in bad code methods.
I've just looked into using:
MyApp myapp = ((MyApp)context.getApplication());
but this won't work unless I can pass the context around, which I'm not sure how to do??
In C# you'd create a static class that could handle this....
Thanks
Neil
You could have:
a static class holding your data (available until the process is killed)
a local service offering access to your data
the application class holding your data
a database mechanism to persist your data (in that case you'd simply pass IDs as intent extra)
All those options are more or less valid, it also depends on what you do with your data, where it comes from, ...
I think you are looking for a Singleton object. Create the custom class that holds all the data as a singleton object and access its data from anywhere in the code. Also, as this data may not be directly related to the UI, you should not be associating it with an Activity.
HTH,
Akshay