deleteDatabse(String) not working android - android

I have an Activity Med.java having the following code:
this.deleteDatabase(db.getDatabaseName());
And another activity OrderDetails.java. But in this activity I have to add the class name also:
OrderDetails.this.deleteDatabase(db.getDatabaseName());
And another activity extending Fragment:
getActivity().deleteDatabase(db.getDatabaseName());
The first two are working fine. But i want to know why in the second activity I have to add the class name. And the third fragment code, it is not working at all. Please help

its using context.deleteDatabase(DATABASE_NAME);
inside a fragment might be when you are calling getActivity() then getActivity() can be null at that time. refer this getActivity() returns null in Fragment function

Related

Android: Pass instance of MainActivity to Fragment

I need the MainActivity-object as context in a Fragment-object.
Passing MainActivity as 'this' to a custom constructor of a Fragment-class works only when starting up the app. When rotating it calls the standard, null-argument-constructor via super.onCreate(savedInstanceState);
Creating a new instance of MainActivity in Fragment does not work either. E.g:
MainActivity ma = new MainActivity();
AdapterTasks at = new AdapterTasks(ma,title, subt, imgid);
-->System services not available to Activities before onCreate()
How can I get a reference from the MainActivity object to a Fragment-object??
If using Java, you can simply call getActivity()
If you're using Kotlin, you can also use requireActivity() which returns a non-null where getActivity() returns nullable, causing you to have lots of null checks in your code.
Within a Fragment, simply call this.getContext().
Or when you need to access something in the parent MainActivity:
MainActivity activity = (MainActivity) this.getActivity();
Both only works while the Fragment is attached to an Activity. Generally, this.getContext() prevents one to add too much code into the Activity, which rather should be added into the Fragment. One can do quite anything in a Fragment, while letting the Activity inflate it.
You don't need to pass the MainActivity.
On your fragment, use this:
activity = (MainActivity) getActivity();

Android - call MainActivity method from Fragment on the second attempt crashes app

Here is what I use to trigger method on MainActvity.java from my Fragment page:
((MainActivity) getActivity()).openGallery();
Once I get to slide in the fragment page for the first time after opening app, and execute this code, IT WORKS. But, when I hide that fragment page and then bring it back, and execute that code again, the app crashes, saying something like:
Attempt to invoke virtual method 'void com.test.test7.MainActivity.openGallery()' on a null object reference
None of the answers I found cover this problem, when it works for the first time and the crashes at the second time.
Any help is apreciated.
try this.
if(getActivity()!=null){
((MainActivity) getActivity()).openGallery();
}
try to use context of that fragment and do something like
((MainActivity) mContext).openGallery();
You can refer to the documentation under Section "Handling the Fragment Lifecycle". Basically somewhere in your code you called ((MainActivity) getActivity()).openGallery() while the fragment is not attached to activity, hence parent context return null.
You should not assume that activity context is always available because more often than not the fragment gets detached and reattached again. You should
(if context is necessary for you) only call getContext() in methods where fragment is guaranteed to be attached (see previous link on available fragment callbacks), or
always perform a conditional check to ensure (MainActivity) getActivity() is not null before use.

Use a dynamic Activity name while calling it's method from a fragment

Ok, so I've a Fragment inside which I use getActivity().getClass().getSimpleName() to get the name of the activity that contains it.
Now, I've a method called sampleMethod() inside that activity, and to call it from the fragment I use ((MyActivity) getActivity()).sampleMethod(); This works fine as well.
My question is that how can I use the activity name in the statement ((MyActivity) getActivity()).sampleMethod(); dynamically. Obviously, I do get the name from getActivity().getClass().getSimpleName().
So what I want is something like
`((getActivity().getClass().getSimpleName()) getActivity()).sampleMethod();
Syntactically, the above is incorrect. What's the correct way?
All the Activities that include this fragment should implement an interface, let's say
interface Sample {
public void sampleMethod();
}
then in your fragment
((Sample)getActivity()).sampleMethod();

What is meant by "Activity this fragment is currently associated with"?

Here in the doc I see that when I call getActivity() from my fragment class, I get the activity object that the fragment is currently associated with.
Suppose if fragment is associated with MainActivity then getActivity() doesn't give an object of the MainActivity class.
I'm unable to understand the difference between Activity activity; and Activity activity = getActivity();
Can anyone please explain me what this "Return the Activity this fragment is currently associated with" actually mean ?
Sorry for asking simple question.
Thanks.
Edit :
Went through this also, got a better understanding.
Association is simple.
Like in real life.
You are a student (Fragment).
You are getting knowledge in Chicago university (MainActivity).
As you are student you will be associated(attached) to university.
And if someone ask you :
-What is your university?(call getActivity() from Fragment)
You will answer :
-I from Chicago university(return object which refered to MainActivity)
If you change the university - you will respond differently.
That's all :)
The simplest answer would be:
"Return the Activity this fragment is currently associated with"
is activity to which this fragment was added.
Then to make things clear.
Activity activity;
This creates reference to activity object, and at start this one is empty (equal to null), until you write some activity object to it.
Activity activity = getActivity();
Here we do 2 things, firstly we create reference to activity object and then we write to this object value of function getActivity(), that if called from fragment will return associated activity.
And lastly why this is not MainActivity? Well it actually may or may not be. :)
And to check if this is actually MainActivity try this :)
Activity activity = getActivity(); // let get our activity
if (activity == null) {
/* this mean we are not attached to activity, possibly, fragment was not shown yet */
}
if (activity instanceof MainActivity) { // we check if activity is actually Main activity.
MainActivity mainActivity = (MainActivity) activity; // and if it is we can do something with it after we cast it.
}
Then at the end i will add one link that might also help: fragment

Is there a way for an Activity to know what fragment was just created?

An Activity may inflate an arbitrary layout xml that may or may not have a Fragment placeholder in it.
If it does , the Fragment will be instantiated and attached to the Activity.
Is there any way to get a reference to the Fragment from the Activity that has been attached to it ?
FragmentManger.findFragmentById() assumes you know the ID in advance to make it work but in this situation I am proposing, it is not available.
The behavior I'd ideally like to have is that the Activity is aware of any Fragments attaching itself to it so that it may respond to it.
Whenever a fragment is attached to an activity the following callback method is called with the fragment attached as the parameter, you can use it store the reference.
onAttachFragment(Fragment fragment)
http://developer.android.com/reference/android/app/Activity.html#onAttachFragment(android.app.Fragment)

Categories

Resources