Get fragment manager from a possible not already instantiated activty - java

I have an application where there are: a splash screen, a main activity (started from the splash screen) where there's a navigationView that attach and detach some fragment.
In one fragment i need to download some information with an AsyncTask. The problem is that the AsyncTask is called in the splash screen (the splash screen survive for 3 seconds) and the download can takes more or less than 3 seconds. In the onPostExecute() in AsyncTask i want to call a function loadFeeback() (that is in the fragment class) only if the fragment is already loaded, if not i set a static variable downloaded to true.
public void onPostExecute(Feedback[] feedbacks){
Dati.feedbacks = feedbacks;
if(mainActivity != null) {
FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
FeedbackFragment feedbackFragment = ((FeedbackFragment) (fragmentManager.findFragmentById(R.id.nav_feedback)));
Dati.feedbackDownloaded = true;
if (feedbackFragment != null)
feedbackFragment.loadFeeback();
}
}
The problem is that i can't get the mainActivity reference and i can't pass the context to the AsyncTask because it is called from the splash screen.
Can anyone help me?

The Asynctask is tied to the splash activity,because it created it.
Maybe you can use a Headless Fragment( fragment without layout):
Create a fragment on precise in the onCreate function :
setRetainInstance(true)
In your splash activity create the fragment and save him with a tag :
HeadlessFragment headlessFragment = new HeadlessFragment()
getSupportFragmentMangager().beginTransaction().add(headlessFragment, "fragmenttag").commit();
Launch your asynctask in the fragment
And when your main activy is launched retrieved the fragment :
HeadlessFragment headlessFragment = getSupportFragmentMangager().findFragmentByTag("fragmenttag");
So the fragment will be attached to the main activity, and you should call whatever you need
Hope this helps.
Sorry for my poor english.

Related

How to call overridePendingTransition() when Activity is destroyed?

I've been tweaking a lot with android design and capability of Android to use the maximum potential of an Android framework, I have come across transition, and my question is how to define Activity's Destroyed's animation ?. Say I am starting an activity using intent like so :
Intent intent_info = new Intent(ComponentsPage.this, SecondActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up, R.anim.no_change);
That snippet basically opens up SecondActivity with Slide Up transition. Now i am in second activity and second activity say doesn't have any button but i want whenever Second Activity is closing (Destroyed) it close with slide down animation.
I've tried adding
overridePendingTransition(R.anim.no_change,R.anim.slide_down);
Inside onDestroy() and onStop(), but still no luck, i guess the activity is already closed when those methods are called.
When you try to finish the second activity try to override the finish method inside your SecondActivity as follows:
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_change, R.anim.slide_down);
}

onBackPressed from Fragment Displays Activity Twice

I have an Application min SDK 21, target 25. Using : android.app.DialogFragment and android.support.v7.app.AppCompatActivity.
The initial Activity is a Launcher it can launch 1 of 3 Activities and 1 DialogFragment. The DialogFragment can in turn launch an Activity (which is one of the 3 Activities). The problem that I have is only with regard to the DialogFragment being loaded and that Fragment then loading an Application and then returning to the Launcher.
When the Fragment returns immediately to the Launcher (without loading the Activity), this works OK. I can detect that the Fragment was loaded via the launcher and onBackPress() handles it OK.
However, when the Fragment loads another Activity and then onBackPress()is used to return to the Fragment and then onBackPress()is used to return to the Launcher, I have a problem. The way this situation is currently handled is that the Fragment loads the Launcher via an Intent using the flags FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP (this appeared to be a solution to someone’s similar problem). I have tried other variations of these flags and also without these flags. I have also tried to use for this situation onBackPress()in the DialogFragment without loading the Activity using an Intent, and the Application terminates, and I need it to return to the Launcher.
In this case, using these flags, when I return to the Launcher, the Launcher displays and then clears and then redisplays (on another onBackPress(), the Application then terminates correctly). The problem is with the Launcher displaying twice. I presume that the Launcher that displays first is the original copy loaded and then it is replaced by a new copy. I have not found a way (in this scenario) in which to load the original copy of the Launcher (if that is in fact what is happening). As stated, this is a DialogFragment.
How can I solve this so that the Launcher Activity does not display twice in this situation?
Why you don't just finish the activity within the fragment to return to the launcher activity?
If you really have to use a intent, try Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
Try overriding the onBackPressed() method as follows:
#Override
public void onBackPressed() {
int count = fragmentManager.getBackStackEntryCount();
if (count == 1) {
finish();
} else {
fragmentManager.popBackStack();
}
}
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
}
use this in onBackPressed() method.

how to make the Fragment/Activity not go to the backgoround

Normally In an Fragment, implicit intent can be used to start a brower component,if there are several brower component, the Android system will show an selection dialog ,the dialog just overlay the Fragment which send the intent.
but in my situation when selection dialog is shown ,the Fragment go to the background(disappeared) ,and the home screen can be seen.
does anyone encounter this problem ,how to make the Fragment not go to the backgoround.
in onPause method it just look like below in my fragment source
#Override
public void onPause() {
mLog.printMethodLifeCycle("onPause");
super.onPause();
}
and in activity I did not overide the onPause method
AndroidManifest.xml is too long ,can you tell me which part should I post or notice

How to stop android system from redeliver old intent on Orientation Change?

I start an activity A from B with an intent with extras and based on the type of extra in onCreate I do some process and it works fine.
But when I do a orientation change the same old intent is re-delivered by the system to me and the whole process gets restarted since I go through the onCreate once again.
My code completely take care of restoring the previous state of the activity when the onCreate gets called if no old intent is delivered to it. But since the system re-delivers it make my activity think it is a new intent and restarts the whole process once again.
I tried the flag intent.getFlags() != Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY. This flag prevents re-delivery in case of long press home button and activity launched from history but no effect on orientation change.
A dirty fix was proposed in another thread Dirty Fix but I am wondering if there is a proper way to address this problem.
Thanks in advance
Use savedInstanceState to determine if the activity or fragment is restored from whatever including orientation change.
savedInstanceState will be null when activity/fragment is restored.
public void onCreate(Bundle savedInstanceState) {
...
boolean isRedeliver = savedInstanceState != null || (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0;
...
}
This technique is also used in official doc of fragment.
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
A 'clean' solution to this problem would be to use Fragments.
Create your own Fragment subclass.
In its onCreate, be sure to call setRetainInstance(true).
In its onCreate you can start your process.
If it is just for doing some work, return null in its onCreateView.
In your Activity's onCreate:
- First try to find the current instance of your own Fragment.
- If not found, create a brand new one and commit this to your Fragment Transaction.
By calling setRetainInstance(true) and by first trying to find it before creating a new Fragment in the Activity's onCreate, you keep the same Fragment instance even after a rotation and the Fragment's onCreate won't be called again.

Android activity restart

Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?

Categories

Resources