Previous activity visible under the new fragment - android

When I call fragment from activity then fragment calling successfully but some part of previous activity is on the top of in fragment, anyone help to correct the code
package com.example.newproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import static android.view.View.GONE;
public class ThankYouPageActivity extends AppCompatActivity {
Button thank_continue_btn,thank_track_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thank_you_page);
getSupportActionBar().setTitle("Confirmation");
thank_continue_btn = findViewById(R.id.thank_continue_btn);
thank_track_btn = findViewById(R.id.thank_track_btn);
findViewById(R.id.thank_continue_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
HomeFragment fragment = new HomeFragment();
fm.beginTransaction().replace(R.id.thanku_container, fragment).addToBackStack(null).commit();

set background to white in the root tag of your fragment's xml
background = "#FFFFFF"

The container of your Fragment must be overlapped by your Activity layouts. Please attach the layouts of your Activity and Fragment so that I can help you find your issue.

I had same issues , i fixed it by removing previous fragment while setting new fragment in oncreateview
container.removeAllViews(); before replacing the fragment

R.id.thanku_container - I think it is FrameLayout?
Make it full match_parent. Or set background color like activity.

Related

Hide the Floating Action Button from my Fragment and keep it hide even I change the orientation of the phone

I could solve how to hide or show a Floating Action Button from a fragment when I call it.
But I faced with another problem that I didn't know how to solve it, when I rotate my phone,
the FAB appears again.
You can see my code below and how I did to hide my FAB, but how to keep it when my phone rotate from
Portrait to Landscape?
package com.example.cursobaralhocigano.ui.deck;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.example.cursobaralhocigano.MainActivity;
import com.example.cursobaralhocigano.R;
import com.example.cursobaralhocigano.classes.cBaralhos;
import com.example.cursobaralhocigano.dao.uLibSql;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
/**
* A simple {#link Fragment} subclass.
*/
public class DeckFragment extends Fragment implements View.OnClickListener {
private uLibSql DB;
private cBaralhos baralho = new cBaralhos();
CheckBox ck01, ck02, ck03, ck04, ck05;
ImageButton Img;
public DeckFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout ln;
View view = inflater.inflate(R.layout.fragment_deck, container, false);
final FloatingActionButton fab = ((MainActivity) getActivity()).findViewById(R.id.fab);
if (fab.isShown()) {
fab.hide();
}
return view;
}
Thanks a lot for help
Regards
Alex
You have to correctly save the instance state of Fragment you should do the following:
In the fragment, save instance state by overriding onSaveInstanceState() and restore in onActivityCreated():
Below link may help you
https://stackoverflow.com/a/17135346/10239870

Fragment getChildFragmentManager() api level < 17

I am very new to Android programming and I was wondering how I am able to fix this error that I'm getting. I have scowwered the internet searching for solutions on how to compensate for the fact that I have api 15 instead of the required 17 for my FragmentTransaction. I tried importing the support.v4.app.FragmentTransaction but still no luck here is the code:
package com.hfad.workout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
private long workoutId;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(savedInstanceState != null)
{
workoutId = savedInstanceState.getLong("workoutId");
}
android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
StopwatchFragment stopwatchFragment = new StopwatchFragment();
ft.replace(R.id.stopwatch_container, stopwatchFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
return inflater.inflate(R.layout.fragment_workout_detail, container, false);
}
}
Any reason as to why the support.v4.app might not be working? Is there a work around to this? Any help is greatly appreciated :D
You have use "import android.app.Fragment" for your fragment.That's why your support.v4.appnot working here.
Try to import android.support.v4.app.Fragment for using android.support.v4.app.FragmentTransaction.
You can try to chaging from
import android.app.Fragment;
public class WorkoutDetailFragment extends Fragment {
to
import android.support.v4.app;
public class WorkoutDetailFragment extends Fragment {

Cannot Resolve Symbol CrimeFragment Android Studio

I am getting a cannot resolve symbol error on CrimeFragment and I don't know why. I am a beginner so I'm not sure how to fix this. Here is the code for CrimeActivity.java
package com.bignerdranch.android.criminalintent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class CrimeActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
Seems like you aren't importing your class CrimeFragment
Make sure that CrimeFragment.java and Crime.java are in the right packages. Select "Packages" view in the top-left corner. If these files hang around separately at the bottom, drag and drop them into the "com.(..).criminalintent" package.

onclick event in fragment activity not working

I have a activity A in which I have a fragment.
In this activity Fragment changes to fragment A(default when activity A is called) or fragment B based on user input in activity A.
In both fragments A & B I have a button with on click listener. but this button works only for the first time when activity A is started.
when user changes fragment the button in those fragments stop responding to on click.
Please suggest what I need to do in order to make buttons in fragment A & B work when fragments are changed by user.
I am replacing fragments based on user input by this code:
fr = new FragmentOneDice();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
In fragment activity code is like this for on click listener button.
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Collections;
public class FragmentOneDice extends Fragment implements View.OnClickListener {
Button button1;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
view = inflater.inflate(R.layout.activity_fragment_one, container, false);
button1 = (Button) view.findViewById(R.id.button_one);
button1.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
//MY CODE HERE
}
The problem was in my activity_main.XML, where I had defined <Fragment> as a placeholder for all fragments and had set one fragment as default. So when other fragment was loaded it was getting overlapped resulting in on click event on the button not working, I changed the <Fragment> to <FrameLayout> as a placeholder. And my problem was solved.
First You remember one thing,
fragment to fragment direct transaction,or fragment to fragment direct replacement is not possible. it is possible only throw the Activity.
Define a interface in your first fragment and make container Activity to implement that interface, so you can send your data from Fragment to Activity .In another hand create Second Fragment and define a interface in it. In Content Activity inside implemented methods of First Fragment interface on that define a Second Fragment ,here we assign a data to the second Fragment through interface.

OnListItemClick Shift from one ListFragement to Another using fragment Transactions

I have a list fragment from where when any list item is clicked, I want to move to another list fragement.
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Schedule extends ListFragment{
....
....
public void onListItemClick(ListView l, View v, int position, long id) {
....
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
DailyList fragment = new DailyList();
fragmentTransaction.replace(R.id.list_fragment, fragment);
fragmentTransaction.commit();
}
This is the new class that I am trying to move to.
public class DailyList extends ListFragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
final View theInflatedView = inflater.inflate(R.layout.day, container, false);
return theInflatedView;
}
....
....
....
}
I tried using v4 android support libraries. but the replace call is giving me this error "The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, DailyList)".
I read few other answers that asked to use v4. That is what I am doing. But still getting error. Please help me out here. Thanks!
I think you are mixing newer fragment support types with support classes. Replace these lines:
import android.app.Fragment;
import android.app.FragmentManager;
with:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Then extend 
android.support.v4.app.FragmentActivity
 instead of 
Activity
 and change calls like 
getFragmentManager()
 with 
getSupportFragmentManager()
 and similarly for other methods that you might be using.

Categories

Resources