Android, strange behavior of fragment transaction replace() - java

I want to use a multipane layout for wider screens. The data is persisted with SQL and each fragment fetches the right data. The extra layout xml files are in resource directory folders.(i.e. layout-w500dp) But i have some strange behavior.
It only seems to work after I select something and then press the back button.
Atm I am using max two FrameLayouts but later I want to do it with four.
I check the level of the deepest selection and assign the fragments accordingly. (Here its only down to lvl 1, but later I need selections up to lvl3).
Here is what I want to achieve.
This gets called in onCreate and when a selection has been made.
private void setScreens(){
int i = getLowestSelection();//returns 0 when nothing is selected.
//And 1 if selection is made in lvl1 ...
int p = 1;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (findViewById(R.id.fragtwo) != null) {
p = 2;
if (i == 1){
SectionsScreen secondFragment = new SectionsScreen();
transaction.replace(R.id.fragtwo,secondFragment);
}
}
if (findViewById(R.id.fragone) != null) {
if(p == 2){
if (i == 0 ){
StatuteScreen statuteScreenFragment = new StatuteScreen();
transaction.replace(R.id.fragone,statuteScreenFragment);
}
}
if (p == 1){
if (i == 0){
StatuteScreen statuteScreenFragment = new StatuteScreen();
transaction.replace(R.id.fragone,statuteScreenFragment);
}
else if (i == 1){
SectionsScreen sectionsScreenFragment = new SectionsScreen();
transaction.replace(R.id.fragone,sectionsScreenFragment);
}
}
}
transaction.addToBackStack(null);
transaction.commit();
}
It only works at the moment if I do the following.
Start application = 1 fragment in portrait and landscape (this is the desired behavior)
Make selection in Portrait = nothing happens !!!! (Here is the problem)
Switch to Landscape = 2 Fragments with the right selection (right behavior) (if I make the initial selection in landscape I need to rotate to Portrait and back again)
Switch to Portrait = LvL 2 Fragment with right Data ( right behavior)
Press Back Button = LvL 1 Fragment (right behavior)
From now on I can switch between portrait to landscape orientation and i get the right behavior for selecting items in all orientations. Even on backpress in landscape showing only one fragment with lvl 1 when selection is taken away.
Why am i getting this behavior?
And is this the right approach in the firstplace?
Considering I want to extend this for further levels and screenWidths!
i.e.:
will backstack function properly here? If anyone needs additional info, just say and i'll be happy to add it!

Silly mistake. I save the selections in an Application Class and i instatiate that in onCreate but i need to reinstantiate before i get the current selection in the getLowestSelection() method

Related

The animation for my programatically created button acts weird in my application

So I am facing a weird bug I cannot explain - I cannot even reproduce it sometimes.
Basic context:
I have an application, which lists objects. Every object has a name and a point value. For every object, the addCustomSpinner function creates a "ticket" (a custom view, kind-of-spinner) and shows them in a scrollview so the user can select the one needed. There are four different 'containers' for four different kind of objects - so the layout can be populated with four kind of "ticket" package.
The data for the objects are collected from a database. The addCustomSpinner is called with a for cycle for every object in the database, and - Important - before the for method, the Layout it populates with the tickets is cleared (removeAllViews).
Inside addCustomSpinner, everything is created as "new" - like the button in question.
addCustomSpinner creates this button and adds a new onClickListener. Inside onClickListener, a new boolean is created - this is used to show a different animation when the button is clicked again. On first click (boolean = true), the arrow turns 180 degrees and faces upwards, on second click (boolean = false) the arrow turns 180 degrees and faces downwards. Works like a charm, until...
The bug I am facing:
Sometimes - as I already mentioned, not every time - if I click the button for one "ticket", then leave it 'opened' and click on an another one, and leave it 'opened' also, THEN I choose to populate the layout with a different kind of "ticket" package - The arrow faces upwards by default on every ticket in every package! Sometimes - again, just sometimes - with the same pattern I can turn it back, but it happens just "by accident".
I don't understand how the animation and state of the buttons can be connected, if every created ticket is new, every button is new, every onClickListener is new, and every boolean inside onClickListener is new. And if these are connected somehow, then why can that be that every behavior is "unique" for the buttons, nothing else shows any connection - even this is just a "sometimes" bug, a pretty rare one.
Can anybody help me why this happens?
What I tried:
Well, tried to trace the issue - but since it happens just by accident, I have no clue, I just searched if I can do anything else than the boolean to add different animation for the clicks. Sadly using ObjectAnimator is not a good solution for me - not the same result at least, since my animated arrow not only rotates, but it also changes its color. Shapeshifter seemed like a good idea to create animations easily, but now as I see it, maybe a simple rotation will be my ultimate solution.
Here's the code for the button:
customButton.setOnClickListener(new View.OnClickListener() {
boolean isCustomButtonClicked = true;
#Override
public void onClick(View v) {
if (isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue_back);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(View.VISIBLE);
isCustomButtonClicked = false;
} else if (!isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(GONE);
isCustomButtonClicked = true;
}
}
});
EDIT:
The full addCustomSpinner():
private void addCustomSpinner(Routes mRouteItemToAdd, String placeName) {
//creating a new View for my custom layout created in xml
View customRoutesView = new View(this);
LinearLayout.LayoutParams customViewParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
customRoutesView.setLayoutParams(customViewParams);
customRoutesView = LayoutInflater.from(this).inflate(
R.layout.custom_view_layout, routeLayout, false
);
//Setting up the views inside the custom view
ImageView imageViewDiffImage = customRoutesView.findViewById(R.id.routeDiffImageView);
TextView textViewRouteName = customRoutesView.findViewById(R.id.routeNameTextView);
TextView textViewRouteDiff = customRoutesView.findViewById(R.id.routeDiffTextView);
ImageButton customButton = customRoutesView.findViewById(R.id.customButton);
RadioButton climberNameOne = customRoutesView.findViewById(R.id.climberNameOne);
RadioButton climberNameTwo = customRoutesView.findViewById(R.id.climberNameTwo);
Button climbedItButton = customRoutesView.findViewById(R.id.climbed_it_button);
RadioGroup climberNameRadioGroup = customRoutesView.findViewById(R.id.climberNameRadioGroup);
RadioGroup climbingStyleRadioGroup = customRoutesView.findViewById(R.id.styleNameRadioGroup);
RelativeLayout routeWhoClimbed = customRoutesView.findViewById(R.id.routeWhoClimbedRelativeLayout);
imageViewDiffImage.setImageResource(R.mipmap.muscle);
textViewRouteName.setText(mRouteItemToAdd.name);
textViewRouteDiff.setText("Difficulty: " + (int) mRouteItemToAdd.difficulty);
climberNameOne.setText(climberName1);
climberNameTwo.setText(climberName2);
routeWhoClimbed.setVisibility(GONE);
//Here comes the button with the animated image
customButton.setOnClickListener(new View.OnClickListener() {
boolean isCustomButtonClicked = true;
#Override
public void onClick(View v) {
if (isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue_back);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(View.VISIBLE);
isCustomButtonClicked = false;
} else if (!isCustomButtonClicked) {
customButton.setImageResource(R.drawable.avd_anim_arrow_blue);
Drawable d = customButton.getDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (d instanceof AnimatedVectorDrawable) {
animArrowAnim = (AnimatedVectorDrawable) d;
animArrowAnim.start();
}
}
routeWhoClimbed.setVisibility(GONE);
isCustomButtonClicked = true;
}
}
});
//Button, works like an 'OK' or something, and I have no
//problem with this
climbedItButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int checkedNameButton = climberNameRadioGroup.getCheckedRadioButtonId();
int checkedStyleButton = climbingStyleRadioGroup.getCheckedRadioButtonId();
RadioButton checkedNameRadioButton = (RadioButton) findViewById(checkedNameButton);
RadioButton checkedStyleRadioButton = (RadioButton) findViewById(checkedStyleButton);
String checkedName = (String) checkedNameRadioButton.getText();
String checkedStyle = (String) checkedStyleRadioButton.getText();
addClimbToDatabase(user.getUid(), checkedName, mRouteItemToAdd, placeName, checkedStyle);
}
});
//And finally, I add this new "ticket" with the custom view to the layout i want to show it. Again, this also works like a charm, no problem here.
routeLayout.addView(customRoutesView);
}
Ultimately, I did not manage to understand the problem throughly, but I was able to eliminate it.
So during my fixing tries I narrowed down the problem to the animated drawable state - credit to #avalerio for his pro tip, but the answer wasn't addig an id to the button. I think somehow and sometime, the state of the first animation (turning the arrow 180 degrees) stuck in the end position - causing the other views using this animatedDrawable showing it in end position on start.
.reset() did not help, since it resets the animatedVectorDrawable object, not the animation xml drawable state. My solution is a kind of workaround, but it is working: when the custom-view 'ticket' is created with the animated-drawable-imagebutton, I set the imageResource of the button to a not-animated xml drawable - this drawable is basically the start position of my animated-drawable. This way, when the 'tickets' are generated, the imagebutton is 'hardcoded' in the start position.
Not elegant, but works. BUT(!) I would really appreciate if someone could explain to me how this weird behavior is possible - just sometimes, randomly, with no pattern I can reproduce intentionally.

How to get the views which are not visible currently in the linear layout of the recycler view

I was designing an Instagram story type template. I am stuck at a very weird problem. I have used recycler view in the main activity.
MainActivity: (This is just the layout shown. I have change the orientation to Horizontal).
My layout:
Then I have designed a custom adapter and in the layout I have used a linearLayout. When clicked on each view It opens a new Activity which shows the whole story content.
Just like in Instagram when a user opens any story, the user can click on the right side of the screen to get to the next story, or left of the screen to get to the previous one. I tried to implement this same functionality. Opening the story was implemented successfully. The problem comes when I added the functionality of right and left click on the screen. I added two button; one to the right and one to the left. The problem is like, if there are currently 3 views visible, then I can navigate in between these stories only and not to the stories which are not visible in the screen because of the recycler view.
The below code is for right and left clicks
leftArrow = findViewById(R.id.leftArrow);
leftArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myAdapter.currentPosition - 1 >= 0) {
int firstVis = MainActivity.linearLayoutManager.findFirstVisibleItemPosition();
MainActivity.linearLayoutManager.scrollToPosition(firstVis - 1);
rightOrLeftClicks(myAdapter.currentPosition - 1);
}
}
});
rightArrow = findViewById(R.id.rightArrow);
rightArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myAdapter.currentPosition + 1 < myAdapter.localDataSet.length) {
int lastVis = MainActivity.linearLayoutManager.findLastVisibleItemPosition();
MainActivity.linearLayoutManager.scrollToPosition(lastVis + 1);
rightOrLeftClicks(myAdapter.currentPosition + 1);
}
}
});
}
Below code is of the function rightOrLeftClicks
public void rightOrLeftClicks(int position) {
finish();
Log.d("rl", "working");
nextView = MainActivity.linearLayoutManager.getChildAt(position);
Log.d("ll", "The last element is " + MainActivity.linearLayoutManager.findLastVisibleItemPosition());
if(nextView != null)myAdapter.onClickView(position, myAdapter.localDataSet[position].symptom, nextView);
}
Below code is for onClickView, It is same code for clicking any view (Story) or clicking the right or left buttons. Basically I just opened the another activity by passing an intent with the next or previous view I obtained when clicked on right or left respectively.
public static void onClickView(int position, String element, View v) {
Intent intent = new Intent(v.getContext(), MainActivity2.class);
idPosition = "";
idPosition = element;
ArrayList<String> passingContent = new ArrayList<>();
currentPosition = position;
passingContent.add(localDataSet[position].description);
passingContent.add(localDataSet[position].imageUrl);
intent.putExtra(element + "", passingContent);
v.getContext().startActivity(intent);
}
The problem is it only gives the visible views to me. Not the rest of the views. I tried auto scrolling so that the next or the previous view become visible but it doesn't seems to work. The last visible position of the child remains the same and it return a null view to me when I try to open the view just after the last visible view.
Assume there are 5 stories. Then I will see only 3 stories in the screen the rest can be seen by scrolling. When I click any of the stories. I can only navigate between 1,2 and 3. When I try to click right from story 3 to see the story 4. The current story is killed by finish() function and null is returned in the nextView variable because of which the story 4 is not loaded and I am returned to the MainActivity.
I have to navigate to all stories present in my recycler view.
Do these changes may help:
1/
I think you should use ViewPager. It automatically snaps to the item without scrollTo to index. Or keep using RecyclerView with the help of SnapHelper
2/
Modify onClickView method not to be static, and don't need View v to work. I see you just need View v just for the context. Why not just pass the context to that method. It's not proper to use a parameter like that, and that approach traps you into 3/ problem.
3/
nextView = MainActivity.linearLayoutManager.getChildAt(position);
You already know how RecyclerView works, just avoid using getChildAt because in some cases, the child you want hasn't been created yet. Base on your code, I think you don't even need to get the nextView
As told by Tam Huynh. The 1st point helped me in getting the new views in the linear layout. The views were working the fine.
There was a problem in the parameter of the function getChildAt. I have to pass the same position I was in previously. Because relatively the position(index) will not change for the child views.
index 0 -> story number 1
index 1 -> story number 2
index 2 -> story number 3
Like if there were story number 1,2 and 3 visible, the index 0 will contain 1st story, index 1 will contain 2nd and index 2 will contain 3rd story. when user clicked the right button on the 3rd story. The 4th story is first visible and now the screen have 2,3 and 4 visible. but the index of the 4th story will remain 3rd only. As now indexing will be like
index 0 -> story number 2
index 1 -> story number 3
index 2 -> story number 4
So, instead of passing position as parameter, currentPosition should be passed in getChildAt

Android OrientationEventListener making to many calls

I have a video on my app and I'm trying to achieve that when the user rotates his device from portrait to landscape the video changes to full-screen.
I'm using OrientationEventListener like this:
orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
#Override
public void onOrientationChanged(int orientation) {
if (orientation <= 45 && playerManager.isFullscreen()) {
onPlayerFullscreenChange(false); //ORIENTATION_PORTRAIT
} else if (orientation <= 135 && !playerManager.isFullscreen()) {
onPlayerFullscreenChange(true); //ORIENTATION_LANDSCAPE
} else if (orientation <= 225 && playerManager.isFullscreen()) {
onPlayerFullscreenChange(false); //ORIENTATION_PORTRAIT
} else if (orientation <= 315 && !playerManager.isFullscreen()) {
onPlayerFullscreenChange(true); //ORIENTATION_LANDSCAPE
}
}
};
The problem is that this listener gets called so many times that my video can't play normally. The activity ends up going throw the OnCreate multiple times unlike before.
The onOrientationChanged(int orientation) method notifies you everytime the sensor detects a change in the way you are holding the Android device.
It is nearly impossible for a user to hold the device still, thus the onOrientationChanged() gets called multiple times.
What I have understood from your question is that, you only want to display a video in fullscreen when the user holds the device horizontally.
Thus consider angular values of 0-45, 135-255, and 315-360 as VERTICAL.
And, angles between 45-135 and 225-315 as HORIZONTAL.
This will make sense if you give it some thought.
Store the "previous orientation", which you can set to null initially for example, if you use String. Then compare it with the currently detected orientation, if they are not the same, take an action (set video to full screen or vice-versa) and save the current orientation values as "previous orientation".
This problem really comes down to better implementing your algorithm. All the best!

Override onOptionsItemSelected in a fragment to not call it in the container activity?

I have a fragment who has 2 faces like it was two separated activities, i want the home button transforms into back arrow when it is in the face two and open the menu when it is in the face 1; the toolbar is outside the fragment. and the code to display the menu is in the container.
public boolean onOptionsItemSelected(MenuItem item) {
// in the container activity "home"
//calls a menĂº i dont want to show it when i'm on the face 2
int id = item.getItemId();
if (face == 2 && id == android.R.id.home) {
show_face(false);//animation true = show face 2; false = show face 1
return true;
}else
return super.onOptionsItemSelected(item);
}
I returned true because of:
"boolean Return false to allow normal menu processing to proceed, true to consume it here."
but i think i misunderstood it.
PD: I want the code be in the fragment for managment purposes because i use too many fragments.
excuse my writing, I'm no good English.

Android - Loading a different Layout depending on value selected from a Spinner

I have a spinner on my home page and when I select a value from the spinner and hit submit my next page loads up with several text boxes.
However, how can I get it so that differnt layouts are displayed according to the value selected from the spinner. I am trying to currently use if statements, however this only works for the first selection, any other causes the app to error out (The application has stopped unexpectedly ... Force close:
String RefType = getIntent().getStringExtra("REFTYPE");
if (RefType.equals("spinner_value_1"))
{
setContentView(R.layout.layoutvalue1);
}
else if (RefType.equals("spinner_value_2"))
{
setContentView(R.layout.layoutvalue2);
}
Any help would be much appeciated.
Thanks
I imagine the error would be because you'd be trying to set the contentView twice, so you could try this:Add int layoutToLoad = 0;*(change here) to the top of your class, this will help us determine what to load. Then in your onCreate:
if (layoutToLoad == 0)
{
setContentView(R.layout.choiceLayout); //whatever the layout with the spinner is
//alternatively you can make the spinner via code
}
else if (layoutToLoad == 1))
{
setContentView(R.layout.layout1);
}
else if (layoutToLoad == 2))
{
setContentView(R.layout.layout2);
}
//etc
then where you're handling your onOptionSelected:
String RefType = getIntent().getStringExtra("REFTYPE");
if (RefType.equals("spinner_value_1"))
{
layoutToLoad = 1;
onCreate(null);
}
else if (RefType.equals("spinner_value_2"))
{
layoutToLoad = 2;
onCreate(null);
}
//etc

Categories

Resources