views are invisible after activity transition ends - android

i recently faced a pretty strange issue with activity transitions. I'm trying to make a slide in transition with exclusions, which are native status bar, native navigation bar and my custom bottom navigation bar. So i created piece of code based on documentation (link here) which in most cases works pretty well, but sometimes all views in the activity have visibility set as View.INVISIBLE (i know that fact from Layout Inspector).
So, i have activity A and activity B. Then i'm navigating from activity A to B via startActivity(). When i click back on activity B sometimes transition works fine, but in some cases all views on activity A (except bottom navigation bar, which was excluded from transition) are set to View.INVISIBLE
My code:
Activity A :
Intent i = new Intent(this, ActivityB.class);
Transition slide = new Slide(Gravity.RIGHT);
slide.excludeTarget(findViewById(R.id.bottom_navigation_bar), true);
slide.excludeTarget(findViewById(android.R.id.navigationBarBackground), true);
slide.excludeTarget(findViewById(android.R.id.statusBarBackground), true);
getWindow().setExitTransition(slide);
startActivity(i,
ActivityOptionsCompat.makeSceneTransitionAnimation(this, createPairs()).toBundle());
private Pair[] createPairs() {
Pair[] pairs = new Pair[2];
pairs[0] = Pair.create(findViewById(android.R.id.statusBarBackground), Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME);
pairs[1] = Pair.create(findViewById(android.R.id.navigationBarBackground), Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME);
return pairs;
}
Activity B:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
setContentView(R.layout.activity);
final View decor = getWindow().getDecorView();
decor.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
decor.getViewTreeObserver().removeOnPreDrawListener(this);
Transition fade = new Fade();
fade.excludeTarget(findViewById(R.id.bottom_navigation_bar), true);
fade.excludeTarget(findViewById(android.R.id.navigationBarBackground), true);
fade.excludeTarget(findViewById(android.R.id.statusBarBackground), true);
getWindow().setEnterTransition(fade);
return true;
}
});

Related

EnterTransition working while exitTransition() not working

Trying to put enter & exit transition to an activity which is opening a fragment. Following are the steps followed.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
TransitionSet enterTransitionSet = new TransitionSet();
enterTransitionSet.addTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));
enterTransitionSet.setDuration(400);
enterTransitionSet.setStartDelay(0);
getWindow().setSharedElementEnterTransition(enterTransitionSet);
Slide enterSlide = new Slide();
enterSlide.setStartDelay(300);
enterSlide.setDuration(300);
getActivity().getWindow().setEnterTransition(enterSlide);
Fade exitFade = new Fade();
exitFade.setStartDelay(300);
exitFade.setDuration(300);
getActivity().getWindow().setExitTransition(exitFade);
}
}
Issue : Enter transition is successfully applied as slide, while the exit transition for window is not reflecting. Activity is exiting with slide effect only without any delay or so.

Drawer Toggle button transitions with fragment navigation

I'm trying to write an app with a toolbar navigation using fragments exactly like the Gmail app: You have a drawer toggle shown as the "hamburger" button, when you click on a mail, the hamburger makes a transition to the back button and vice-versa.
As of now, I've been able to achieve something very close to what I want, except for the toggle button that is not "animating" from Hamburger to back arrow.
What I did is to bind a Listener for the BackStack in the mainActivity:
SupportFragmentManager.AddOnBackStackChangedListener(this);
Then from Fragment A, I can load fragment B adding it to the Back Stack:
ResultFragment fragment = new ResultFragment();
this.Activity.SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.main_fragment, fragment)
.AddToBackStack("results")
.Commit();
In the Main Activity the Listener checks for Backstack, if is not empty it switches the hamburger to the backbutton:
bool canGoBack = SupportFragmentManager.BackStackEntryCount > 0;
if (canGoBack)
{
//Showing Back Button
if (!_toolbarNavigationListererSet)
{
drawerToggle.DrawerIndicatorEnabled = false;
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
BackHandler backHandler = new BackHandler(this);
drawerToggle.ToolbarNavigationClickListener = backHandler;
_toolbarNavigationListererSet = true;
}
}
else
{
//Show the hamburger
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
drawerToggle.DrawerIndicatorEnabled = true;
drawerToggle.ToolbarNavigationClickListener = null;
_toolbarNavigationListererSet = false;
}
The drawerToggle.ToolbarNavigationClickListener changes the behaviour of the back button to call the back button press event, like:
Activity.OnBackPressed();
nothing more.
I understand that by doing this, the hamburger is hidden and the back button is shown, and viceversa, so I'm sure that's the reason why I cannot see the animation.
What am I missing? Thank you for your help.
P.s. The code is written in C# as I'm using Xamarin.Android but Java code and/or Android Native Code is well accepted as a suggestion.
You could add a animation when you press the back button, like this :
ValueAnimator anim = ValueAnimator.OfFloat(0f, 1.0f);
anim.AddUpdateListener(new AnimatorUpdateListener(this));
anim.SetInterpolator(new DecelerateInterpolator());
anim.SetDuration(500);
anim.Start();
public class AnimatorUpdateListener : Java.Lang.Object, ValueAnimator.IAnimatorUpdateListener
{
private MainActivity mContext;
public AnimatorUpdateListener(MainActivity context)
{
mContext = context;
}
public void OnAnimationUpdate(ValueAnimator valueAnimator)
{
var slideOffset = (System.Single)valueAnimator.AnimatedValue;
mContext.drawerToggle.OnDrawerSlide(mContext.drawerLayout, slideOffset);
}
}

Android Design Support Library - toolbar transition

I try to make android toolbar transition from recyclerview item name to toolbar title (text). I create it like Alex Lockwood recommendation. In appcompat-v7:22 all works fine, when I update to appcompat-v7:23 transition does't work.
Does somebody know whats happen with appcompat-v7:23?
My code (with appcompat-v7:22 all works fine):
private void startDataActivity(Activity activity, View toolbar,
Category category) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decor = activity.getWindow().getDecorView();
View statusBar = decor.findViewById(android.R.id.statusBarBackground);
View navBar = decor.findViewById(android.R.id.navigationBarBackground);
List<Pair> participants = new ArrayList<>(3);
participants.add(new Pair<>(toolbar, activity.getString(R.string.transition_toolbar)));
addNonNullViewToTransitionParticipants(statusBar, participants);
addNonNullViewToTransitionParticipants(navBar, participants);
ActivityOptions sceneTransitionAnimation = ActivityOptions
.makeSceneTransitionAnimation(activity,
participants.toArray(new Pair[participants.size()]));
final Bundle transitionBundle = sceneTransitionAnimation.toBundle();
activity.startActivity(ShowCategoryActivity.getStartIntent(activity,
category.getId()), transitionBundle);
} else {
activity.startActivity(ShowCategoryActivity.getStartIntent(activity,
category.getId()));
}
}

WebView not visible on window

I have a WebView that his isShown method return false even i set visibility View.VISIBLE... googling i found that view probably is not visible on window, i use this code to verify this, and in fact is not visible on window.
Rect scrollBounds = new Rect();
web.getHitRect(scrollBounds);
if (web.getLocalVisibleRect(scrollBounds))
{
Log.d("STATE", "At least 1 px is visible");
}
else
{
Log.d("STATE", "I completaly dissapear fool");
}
How can make the view visible on window?
Have you added the WebView to your view hierarchy? If you new it up
web = new WebView(...);
you need to add it to a parent view:
parent.addView(web);
or set it as the contents of your activity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
web = new WebView(...);
setContentView(web);
}

Why does my animation freeze when returning to activity containing it?

I'm still sort of new with Android, so forgive me if it's an obvious mistake. In this activity I'm using ViewPager to horizontally scroll through three layouts containing an ImageButton that has an animated background depending on its current state. When the button is pressed, it starts a new activity. However, when I hit the back button to go back to the activity containing the animation from the new activity, sometimes the animation freezes or plays back faster than it should. I wrote a method for starting up the animation that I use in onWindowFocusChanged(), and onRestart(). I'm working in Android 2.1 (API 7).
This is my code:
public class CopyOfWorld extends Activity{
MediaPlayer muzak;
Boolean mSwitch = false;
ImageButton holmes;
AnimationDrawable holmesAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.world);
Preferencer pp = (Preferencer)getApplicationContext();
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.viewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
if(pp.getMuzak()){
mSwitch = true;
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
}
public void clicker(View v){
Intent myIntent = new Intent(CopyOfWorld.this , Subworld.class);
startActivityForResult(myIntent, 0);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
beginRender();
}
#Override
protected void onPause(){
super.onPause();
if(mSwitch){
muzak.release();
}
}
#Override
protected void onRestart(){
super.onRestart();
Preferencer pp = (Preferencer)getApplicationContext();
if(pp.getMuzak()){
muzak = MediaPlayer.create(CopyOfWorld.this, R.raw.level1);
muzak.setLooping(true);
muzak.start();
}
beginRender();
}
public void beginRender(){
ImageButton holmes = (ImageButton) findViewById(R.id.subworlder);
StateListDrawable background = (StateListDrawable) holmes.getBackground();
Drawable current = background.getCurrent();
if(current instanceof AnimationDrawable){
holmesAnimation = (AnimationDrawable) current;
holmesAnimation.start();
}
}
}
I've tried calling the method beginRender() under ()onResume, but then the app simply crashes.
Could anyone point me in the right direction?
EDIT:
I've been tweaking the code here and there, unfortunately to no avail. But I did notice a pattern in the behavior of the animation. When I press down on the ImageButton or hold it so that it goes from its default animation to its pressed or focused animation then move my finger away from the button so that it doesn't start up the new activity, it sometimes behaves very much as I described at the beginning of this post (i.e. it's supposed to return to its default animation, but instead plays back at twice the rate, chokes up, or doesn't play at all.)
Currently the xml that contains these ImageButtons defines their backgrounds as the animations and have no source (src). But when I change the background to transparent and the src to the animations, the app crashes.
Any clues?

Categories

Resources