why method getBackStackEntryCount always return 0 - java

I need to count my fragment, I used getbackstackentrycount, but it always return 0. Can someone tell me whats wrong with my code?
here my code MainActivity.java
package com.example.bams.taskagain;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.bams.taskagain.Fragment.FragmentOne;
public class MainActivity extends AppCompatActivity {
Button btn;
FragmentManager fm = getFragmentManager();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.fragOne);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fm.beginTransaction().add(R.id.content, new FragmentOne()).addToBackStack("aa").commit();
//Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
}
});
}
}
and here is my fragment class
package com.example.bams.taskagain.Fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bams.taskagain.R;
/**
* Created by jack on 25/02/2017.
*/
public class FragmentOne extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Total back stack "+ ((AppCompatActivity)getActivity()).getSupportFragmentManager().getBackStackEntryCount());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one,container,false);
return rootView;
}
}
can someone help me with my code?
I just want to get the BackStack count that my fragement has.

You're using android.app.Fragment and android.app.FragmentManager but using getSupportFragmentManager() to get back stack entry count.
Use
getFragmentManager().getBackStackEntryCount()
getFragmentManager() is used if your fragment is android.app.Fragment and getSupportFragmentManager() if your fragment is android.support.v4.fragment.
For more info on these fragments, take a look at Difference between android.app.Fragment and android.support.v4.app.Fragment

Related

Fragment translation and phone orientation bug

Phone's orientation and fragment translation bug.
I have an activity and some fragments. The first fragment comes right away over the activity with a logo and after 3 seconds the second fragment translates over. The problem is if I change the orientation of the phone, the 1st fragment reappears with the same delay and same behavior as I start the app.
MainActivity:
package com.mainpackage.pinbook;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.mainpackage.pinbook.com.autentification.LoginFragment;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container , new MainScreen());
fragmentTransaction.commit();
}
}
MainScreen:
package com.mainpackage.pinbook;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mainpackage.pinbook.com.autentification.*;
public class MainScreen extends Fragment{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen , container , false);
}
private TextView entry_text;
public static final String TAG = MainScreen.class.getSimpleName();
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
slide();
}
}, 3000);
}
private void slide(){
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(0,R.anim.slide_in_left);
fragmentTransaction.replace(R.id.main_container , new LoginFragment());
fragmentTransaction.commit();
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
}
The second fragment can be blank.
What I want is simply to remain on the current fragment when I change the phone's orientation
When the device rotates onCreate() gets called again.
Wrap your fragment transaction in onCreate() like so:
if (savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container , new MainScreen());
fragmentTransaction.commit();
}
This will prevent the first fragment from being put back on top when you rotate.

app getting crashed when trying to use setOnClickListener

main activity
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button(View v)
{
FragmentManager fragmentManager=getFragmentManager();
frag1 f1=new frag1();
f1.show(fragmentManager,"TAG");
}
}
fragment
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class frag1 extends DialogFragment implements View.OnClickListener{
Button button;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag1,container,false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button= (Button) getActivity().findViewById(R.id.button);
button.setOnClickListener((View.OnClickListener) getActivity());
}
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"working",Toast.LENGTH_LONG).show();
}
}
This is a code for a simple dialog box.
Here's a link where I learnt it from. It works completely fine if I just create the dialog box with calling the button.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.frag1,container,false);
button= (Button) View.findViewById(R.id.button);
button..setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(getActivity(),"working",Toast.LENGTH_LONG).show();
}
});
return view;
}
Change your line from
button.setOnClickListener((View.OnClickListener) getActivity());
to
button.setOnClickListener(this);
Also, use
getSupportFragmentManager() to instead of getFragmentManager()
and import
import android.support.v4.app.FragmentManager;

implements TopSectionFragment Cannot resolve symbol android studio

public class MainActivity extends AppCompatActivity implements
TopSectionFragment.TopSectionListener {
When I try to implement the TopSectionFragment the writing goes red and, then it says Cannot resolve symbol when my mouse goes over it.
This is all happening in Android Studio.
My MainActivity.java looks like this:
package com.example.danielhunter.fragments;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;`
public class MainActivity extends AppCompatActivity
implements TopSectionFragment.TopSectionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My TopSectionFragment.java looks liks this:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;
import android.support.v4.app.Fragment;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import com.example.danielhunter.fragments.R;
public class TopSectionFragment extends Fragment {
private static EditText topTextInput;
private static EditText bottomTextInput;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void createMeme(String top,String bottom);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
activityCommander = (TopSectionListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString());
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);
topTextInput = (EditText) view.findViewById(R.id.topTextInput);
bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
buttonClicked(v);
}
}
);
return view;
}
// calls this when the button is clicked
public void buttonClicked(View view){
activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());
}
}
In order for a class to correctly implement an interface, it must include implementations of ALL the methods defined in the interface. In your case, MainActivity would need to have a method with the signature void createMeme(String top,String bottom) in order for this to compile.

cannot resolve method show(android.support.v4.app FragmentManager,java.lang.string)

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
/**
* Created by jamie on 9/12/2015.
*/
public class PlayersFragment extends DialogFragment {
ListView lv;
String[] players = {"arteta", "costa", "reid", "degea", "rooney", "terry"};
int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
R.drawable.rooney, R.drawable.terry};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//set dialog title
getDialog().setTitle("Soccer SuperStars");
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players, images);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Trying to create a ListView fragment in android studio and getting
error
cannot resolve method show(android.support.v4.app
FragmentManager,java.lang.string)
the error is on the p.show(fm,"Players Fragment) underlined in red, tried to resolve this but getting nowhere, i would really appreciate a solution to this! thank you
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
Button showBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentManager fm =getSupportFragmentManager();
final PlayersFragment p=new PlayersFragment();
showBtn=(Button)findViewById(R.id.button1);
showBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
p.show(fm,"Players Fragment"); //error is here
}
});
}
}
I suppose you are using android.app.DialogFragment and not android.support.v4.app.DialogFragment. Just extend PlayersFragment from support library's android.support.v4.app.DialogFragment. Or, if you are not targeting old devices, you can change getSupportFragmentManager() to getFragmentManager()
You have to import android.support.v4.app.DialogFragment on the class PlayersFragment and then change getSupportFragmentManager() to getFragmentManager().

Passing values between fragments while using viewpager in android

I want to create a sign up form where the form has been divided into three fragments.There is a next button in the fragment which causes the viewpager to move to the next fragment.But i want to pass values between the fragments how do i do that?
The main class:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends FragmentActivity {
ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setAdapter(pageAdapter);
}
public void selectFragment(int position){
pager.setCurrentItem(position, true);
// true is to animate the transaction
}
}
The first fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentBlue extends Fragment {
Button btnnext1;
RelativeLayout frag1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blue, container, false);
btnnext1=(Button) view.findViewById(R.id.btnnext1);
frag1=(RelativeLayout) view.findViewById(R.id.frag1);
btnnext1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(1);
}
});
return view;
}
}
The second fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentGreen extends Fragment {
Button btnnext2;
RelativeLayout frag2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_green, container, false);
btnnext2=(Button) view.findViewById(R.id.btnnext2);
frag2=(RelativeLayout) view.findViewById(R.id.frag2);
btnnext2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(2);
}
});
return view;
}
}
The third fragment:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentPink extends Fragment {
Button btnnext3;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pink, container, false);
btnnext3=(Button) view.findViewById(R.id.btnnext3);
btnnext3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}
}
I would place a submit button in the last fragment which would post data to the server,So the data in the first two fragments should be available in the last fragment.So i want to know how to pass data from the first two fragments to the last one?
Also is my approach a bad way of achieving the task?Would i be better of creating three different activities and using intent and bundle to pass data?
You can use SharedPreferences. Before you change the fragment save entered values:
SharedPreferences sp = getSharedPreferences("pref_name", MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPref.edit();
editor.putString("key", value);
editor.commit();
And in other fragment when you want to use saved value:
SharedPreferences sp = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE);
String value = mSharedPref.getString("key", null);
if(value !=null){
//do something with value
}
Another approach is using interface-callback pattern. You can read more here.

Categories

Resources