I'm trying to get this example code to work in Java:
https://developer.android.com/training/basics/intents/result
private final ActivityResultLauncher<Void> mTakePicture =
registerForActivityResult(new TakePicturePreview(), mRegistry, new ActivityResultCallback<Bitmap>() {
#Override
public void onActivityResult(Bitmap thumbnail) {
mThumbnailLiveData.setValue(thumbnail);
}
});
The example happens to be a "Fragment", and it gets mRegistry from the constructor:
public class MyFragment extends Fragment {
private final ActivityResultRegistry mRegistry;
...
public MyFragment(#NonNull ActivityResultRegistry registry) {
super();
mRegistry = registry;
}
My test case is an Activity (the "MainActivity"), not a Fragment:
public class MainActivity extends AppCompatActivity {
...
private ActivityResultRegistry activityResultRegistry;
Q: How can I initialize my registry ("activityResultRegistry") in this scenario?
It was easier than I thought:
private ActivityResultRegistry activityResultRegistry = this.getActivityResultRegistry();
Related
For instance I want to take this
public class SomeClass {
public static final String GREET_STRING = "Hello!";
//...
and change it to something like:
public class SomeClass {
public static final String GREET_STRING = getString(R.string.greet_string);
//...
Can this be done or do I need some kind of Context instantiation to get the resources for the string loading?
To use getString() you will need a context. A Resource string cannot be static final because it is possible for String resources to change as you change Locales (if you have multiple String files such as strings.xml (us) and strings.xml (uk))
Try this:
public abstract class SomeClass extends AppCompatActivity {
public static String GREET_STRING(Context context) {
if (context == null) {
return null;
}
return context.getResources().getString(R.string.greet_string);
}
}
Res/Value/String:
<resources>
<string name="greet_string">Hello!</string>
</resources>
Call SomeClass from MainClass
public class MainClass extends SomeClass {
private final static String TAG = MainClass.class.getName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// call SomeClass from MainClass
Log.i(TAG, SomeClass.GREET_STRING(this));
}
}
There are two ways access the string inside the class which is not extending Activity or Fragment.
Pass Context or Activity to class constructor
public class SomeClass {
private Context context;
public SomeClass(Context context) {
this.context = context;
}
public static final String GREET_STRING = context.getString(R.string.greet_string);
}
The second way is if you don`t want to pass context to class. You need to create an instance of the Application and static function get instance.
public class App extends Application {
private static App instance = null;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static App getInstance() {
// Return the instance
return instance;
}
}
public class SomeClass {
public static final String GREET_STRING = App.getInstance().getString(R.string.greet_string);
}
I need to have have reference to Context in my utils class.
First I am extending Application class and initializing my util class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Utils.init(getApplicationContext());
}
}
And utils class looks like:
public class Utils{
private static Context sContext;
private Utils() {
}
public static void init(Context context) {
sContext = context;
}
}
Is there any possible way to get a leak with such approach?
I can see only one case: when application goes background - Context can be re-created, and so Utils class may be re-initialized even if it will persist in memory.
Any suggestions, please.
You should solve as follow:
public class YourClass extends Application {
private static Context context;
public void onCreate()
{
super.onCreate();
YourClass.context = getApplicationContext();
}
public static Context getAppContext() {
return YourClass.context;
}
}
How to use:
YourClass.getAppContext();
The Parcelable docs say that the CREATOR field must be static, but when I try to implement it this way I get the error "Inner classes cannot have static declarations".
I attempted to resolve this by putting my CategoryButton in a separate class (not declaring it as an inner class in MainActivity) but then I couldn't call getApplicationContext() in the constructor to pass to the super.
public class MainActivity extends ActionBarActivity {
private class CategoryButton extends Button implements Parcelable{
private ArrayList<CategoryButton> buttons = null;
private RelativeLayout.LayoutParams params = null;
public CategoryButton(Context context){
super(context);
};
public void setButtons(ArrayList<CategoryButton> buttons){
this.buttons = buttons;
}
public void setParams(RelativeLayout.LayoutParams params){
this.params = params;
}
public ArrayList<CategoryButton> getButtons(){
return this.buttons;
}
public RelativeLayout.LayoutParams getParams(){
return this.params;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeList(buttons);
}
public static final Parcelable.Creator<CategoryButton> CREATOR // *** inner classes cannot have static declarations
= new Parcelable.Creator<CategoryButton>() {
public CategoryButton createFromParcel(Parcel in) {
return new CategoryButton(in); // *** 'package.MainActivity.this' cannot be referenced from a static context
}
public CategoryButton[] newArray(int size) {
return new CategoryButton[size];
}
};
private CategoryButton(Parcel in) {
super(getApplicationContext());
in.readList(buttons, null);
}
}
// ...other activity code
You need to set your CategoryButton as inner static, i.e.
private static class CategoryButton extends Button implements Parcelable {
...
How can I get the value of "somevariable" from the "OtherClass"? Can/Should I use the Activity's context for this as I was trying below?
public class ParentActivity extends Activity {
//This variable is reused by multiple Activities inheriting form this class
protected static String somevariable = "text";
...
}
public class MyActivity extends ParentActivity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
myObject = new OtherClass(this);
myObject.doSomething();
...
}
...
}
public class OtherClass(){
private Context c;
private String b;
OtherClass(Context context) {
c = context;
}
doSomething() {
// This does NOT work.
// How can I get somevariable from the ParentActivity????
b = c.somevariable;
}
}
If you want to access the specific somevariable defined in class ParentActivity then you should try to access it as ParentActivity.somevariable since it is declared static:
public class OtherClass { // Why were there extra parentheses ()
private Context c;
private String b;
OtherClass(Context context) {
c = context;
}
doSomething(){
b = ParentActivity.somevariable; // Try this at home ;)
}
}
a = ParentActivity.somevarible;
that should work.
how can I trigger AsyncTasks that are contained in another activity from my main activity?
public class DatabaseActivity extends Activity {
private class DbReader extends AsyncTask<..> {
#Override
protected List<MyData> doInBackground(..) {
//execute query etc
}
}
private class DbSaver extends AsyncTask<..> {
#Override
protected void doInBackground(MyData data) {
//save to dn
}
}
private class DbRemover extends AsyncTask<..> {
#Override
protected void doInBackground(MyData data) {
//remove in db
}
}
}
How can I trigger from MyApplication extends Actitivy?
i think you should use seperate class where AsyncTask is alone.
when your app needs more than one AsyncTask then you should use seperate AsyncTask and call it.
private class CommonTask extends AsyncTask<..> {
public CommonTask(Foo foo){
}
#Override
protected void doInBackground(MyData data) {
//remove in db
}
}
No you can pass diffrent value for constructor and check what you want from Activity either data save or remove or anything else...