I have two ACTIVITY (A and B). I have a FRAGMENT F in ACTIVITY A. My Fragment F cointains 3 EditText (1,2,3).
My app gets a string from Activity B and places it in EditText 3. I have no problem with that. My problem is, when i type something in EditText 1 and 2 then I'll get the string from ACTIVITY B and put it in EditText 3, all of the information I typed in EditText 1 and 2 are gone.
My question is, how will the information I typed in EditText 1 and 2 stay even though I get the string from ACTIVITY B to EditText 3. Here's my code:
ACTIVITY A
public class ActivityA extends FragmentActivity {
ViewPager viewPager = null;
PageIndicator pIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
pIndicator = (PageIndicator)findViewById(R.id.indicator);
pIndicator.setViewPager(viewPager);
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i == 0)
{
fragment = new FragmentF();
}
if (i == 1)
{
fragment = new FragmentG();
}
if (i == 2)
{
fragment = new FragmentH();
}
if (i == 3)
{
fragment = new FragmentI();
}
return fragment;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0)
{
return "TAB.F";
}
if (position == 1)
{
return "TAB.G";
}
if (position == 2)
{
return "TAB.H";
}
if (position == 3)
{
return "TAB.I";
}
return null;
}
}
public void btn_to_actb(View view) {
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
ACTIVITY B
public final static String EXTRA_MESSAGE = "com.sample.MESSAGE";
// onClick get button from activity B layout
public void get(View view) {
Intent intent = new Intent(this, ActivityA.class);
TextView textView = (TextView)findViewById(R.id.coordinates);
String message = textView.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
FRAGMENT F
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String num = getActivity().getIntent().getStringExtra("com.sample.MESSAGE");
View v = inflater.inflate(R.layout.fragmentf, container, false);
// EditText3 from fragment F layout
editText = (EditText) v.findViewById(R.id.edittext3);
editText.setText(num);
return v;
}
Notice that when you launch your intent to Activity A from B, the Activity A could be recreated o resumed depending on if A was stopped or if it was still on memory.
Your fragment F could be recreated or resumed too depending on how you manage the fragment in your Activity A, so you would lose all the information stored in editTexts.
It would be usefull if you post the code of your Activity A.
The best way to keep the data of fragments between transitions, is store it in your Activity and get it from the Fragment.
In your Fragment
#Override
public void onResume(){
EditText1.setText(getActivity().getTextOfEditText1());
EditText2.setText(getActivity().getTextOfEditText2());
super.onResume();
}
** And in your Activity**
public class A extends Activity{
//...
private String textOfEditText1;
private String textOfEditText2;
//... Other stuff
public String getTextOfEditText1(){
return textOfEditText1;
}
public String getTextOfEditText2(){
return textOfEditText2;
}
}
You should have methods to set the Strings from the Fragment when you modify them.
Hope it helps :)
Related
This is code of onboarding fragment 3.in In this code, I have set on click listener to the floating action button fb and tried to save the state of the button clicked.
public class onBoardinFragment3 extends Fragment {
FloatingActionButton fab;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_onboarding3, container,false);
**fab = root.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);**
**SharedPreferences prf = getActivity().getApplicationContext().getSharedPreferences("mypref",MODE_PRIVATE);
SharedPreferences.Editor editor = prf.edit();
editor.putBoolean("isOpened",true);
editor.commit();**
}
});
return root;
}
}
This the code of introductory Activity. The value of
isIntroductoryOpened doe nor become false after clicking the floating
button in onboarding fragment 3.
public class IntroductoryActivity extends AppCompatActivity {
ImageView BgImage;
LottieAnimationView lottieAnimationView;
private static final int Num_Pages = 3;
private ViewPager viewPager;
private ScreenSlidePagerAdapter pagerAdapter;
Animation anim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
**if (isIntroductoryOpened()){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}**
setContentView(R.layout.activity_introductiory);
BgImage = findViewById(R.id.bgImage);
lottieAnimationView = findViewById(R.id.lottie);
BgImage.animate().translationY(-4000).setDuration(1000).setStartDelay(4000);
lottieAnimationView.animate().translationY(1400).setDuration(1000).setStartDelay(4000);
viewPager = findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
anim = AnimationUtils.loadAnimation(this,R.anim.customanim);
viewPager.startAnimation(anim);
}
**private boolean isIntroductoryOpened() {
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
Boolean isIntroOpened = pref.getBoolean("isOpened",false);
return isIntroOpened;
}**
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
onBoardinFragment1 tab1 = new onBoardinFragment1();
return tab1;
case 1:
onBoardinFragment2 tab2 = new onBoardinFragment2();
return tab2;
case 2:
onBoardinFragment3 tab3 = new onBoardinFragment3();
return tab3;
}
return null;
}
#Override
public int getCount() {
return Num_Pages;
}
}
}
In the activity before OnBoarding activity, change the code as:
SharedPreferences onBoarding = getSharedPreferences("onBoardingScreen",MODE_PRIVATE);
boolean isFirstTime = onBoarding.getBoolean("firstTime",true);
if(isFirstTime){
SharedPreferences.Editor editor = onBoarding.edit();
editor.putBoolean("firstTime",false);
editor.commit();
Intent intent = new Intent(PrevActivity.this,OnBoarding.class);
startActivity(intent);
}
else{
Intent intent = new Intent(PrevActivity.this,NextActivity.class);
startActivity(intent);
}
PrevActivity is the activity before OnBoarding and NextActivity is the activity after OnBoarding acitivity.
In second line the true value is the default value for firstTime, once getting in the if block, the value will be set to false and the OnBoarding activity will not be shown again to the user.
This code will come in place of the code you have written for going from PrevActivity to the OnBoarding activity.
i implements my interface in Two fragments i want to send Data from Activity to both Fragments..when i do this my data send in Second Fragment not in First Fragment. it called only Second Fragments interface method..not call first Fragment interface method i d'nt UnderStand Please Some One Help....
This is Main Activity
public class MainActivity extends AppCompatActivity implements FragmentCommunication {
TextView textView;
ReceiveMessage receiveMessage;
EditText editText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textAcitivity);
editText = findViewById(R.id.editActivity);
button = findViewById(R.id.buttonActivity);
if (findViewById(R.id.fragment1)!= null)
{
if (savedInstanceState != null)
{
return;
}
}
FirstFragment firstFragment = new FirstFragment();
SecondFragment secondFragment = new SecondFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction()
.add(R.id.fragment1, firstFragment, null);
fragmentTransaction.commit();
FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction().add(R.id.fragment2, secondFragment,null);
fragmentTransaction1.commit();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = editText.getText().toString().trim();
receiveMessage.receiveMessage(name, 3);
}
});
}
#Override
public void onAttachFragment(#NonNull Fragment fragment) {
super.onAttachFragment(fragment);
try {
receiveMessage = (ReceiveMessage) fragment;
} catch (ClassCastException e) {
throw new ClassCastException(fragment.toString() + " must implement receiveMessage..");
}
}
}
This is my interface
public interface ReceiveMessage {
void receiveMessage(String message, int from);
}
This is my First Fragment
public class FirstFragment extends Fragment implements ReceiveMessage {
private View view;
private TextView textView;
private EditText editText;
private Button button;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.firstfragment, container, false);
textView = view.findViewById(R.id.textView10);
editText = view.findViewById(R.id.Edit1);
button = view.findViewById(R.id.button1);
return view;
}
#Override
public void receiveMessage(String message, int from) { // THIS METHOD NAVER CALLED WHEN I SEND dATA
FROM aCTIVITY
if (from == 3)
{
textView.setText(message);
}
}
}
This is my Second Fragment
public class SecondFragment extends Fragment implements ReceiveMessage {
private View view;
private TextView textView;
private EditText editText;
private Button button;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.secondfragment, container, false);
textView = view.findViewById(R.id.textView2);
editText = view.findViewById(R.id.editText2);
button = view.findViewById(R.id.button);
return view;
}
#Override
public void receiveMessage(String message, int from) {
if (from == 3)
{
textView.setText(message);
}
}
}
Every time when you add a new fragment, the "onAttachFragment" of activity gets called again. So when you added the second fragment, you resetted the value of "receiveMessage" object to the interface implementation of second fragment.Hence you should rewrite your code as :
ReceiveMessage firstReceiver;
ReceiveMessage secondReceiver;
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment instanceof FirstFragment) {
firstReceiver = (ReceiveMessage) fragment;
} else if (fragment instanceof SecondFragment) {
secondReceiver = (ReceiverMessage) fragment;
}
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = editText.getText().toString().trim();
firstReceiver.receiveMessage(name, 3);
secondReceiver.receiveMessage(name, 3);
}
});
After following a couple of Youtube tutorials (eg https://www.youtube.com/watch?v=tPV8xA7m-iw) I was able to build an Activity with a BottomNavigationView, that switches between three different fragments.
I'm not sure if this is the best method to achieve my goals. I need three screens, one of which (ReportFragment) will have user entered data that I wish to remaining place as the user switches between fragments.
The difficulty I am having is also retaining the Fragment data on an orientation change.
Here is one of the Fragment codes, I have implemented onSavedInstanceState to recall the data the user has entered and it seems to work okay:
public class ReportFragment extends Fragment {
EditText textCheck;
EditText textCheck2;
EditText textCheck3;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_report, null);
textCheck = view.findViewById(R.id.editName);
textCheck2 = view.findViewById(R.id.editHaz);
textCheck3 = view.findViewById(R.id.editNotes);
if (savedInstanceState != null) {
textCheck.setText(savedInstanceState.getString("name"));
textCheck2.setText(savedInstanceState.getString("haz"));
textCheck3.setText(savedInstanceState.getString("notes"));
}
return view;
}
#Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString("name", textCheck.getText().toString());
outState.putString("haz", textCheck2.getText().toString());
outState.putString("notes", textCheck3.getText().toString());
}
}
Here is the Activity Code:
public class FeedActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener {
private HomeFragement frag1;
private ReportFragment frag2;
private MapFragement frag3;
BottomNavigationView navigation;
private static final String TAG = "FeedActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
frag1 = new HomeFragement();
frag2 = new ReportFragment();
frag3 = new MapFragement();
loadFragment(frag1);
}
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
//.addToBackStack(null)
.commit();
return true;
}
return false;
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = frag1;
break;
case R.id.navigation_camera:
fragment = frag2;
break;
case R.id.navigation_map:
fragment = frag3;
break;
}
return loadFragment(fragment);
}
}
I have tried a couple of ways in an attempt to retain the original Fragment, one being onSavedInstanceState, as per other suggestions on Stack Overflow. However during execution I receive an error regarding a null reference to navigation:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("opened_fragment", navigation.getSelectedItemId());
}
as well as using the following code up in the OnCreate Method in in the if(onSavedInstanceState == null) statement:
navigation.setSelectedItemId(savedInstanceState.getInt("opened_fragment"));
The app currently correctly switches between the three fragments and retains the data as it needs to, until an orientation change occurs. The displayed fragment reverts back to frag1, and can no longer switch between fragments (I'm guessing it is something to do with the setOnNavigationItemSelectedListener??).
I am new to Android development.
EDIT: Now using a ViewPager which handles the the issues above
I believe I have found a solution myself. It appears to work, and give the functionality I need. However I would love some feedback from any experts as to whether it is the right solution.
The solution uses the fragment 'tag' assigned when using getSupportFragmentManager().replace, and checks if there has been a previous version of the fragment loaded. I'm not confident my logic in onNavigationItemSelected() is great but would love any feedback/criticism
Updated FeedActivity.Java:
public class FeedActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener {
private ReportFragment frag2;
private MapFragement frag3;
private HomeFragement frag1;
BottomNavigationView navigation;
private static final String TAG = "FeedActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
if(savedInstanceState==null) {
frag1 = new HomeFragement();
frag2 = new ReportFragment();
frag3 = new MapFragement();
}
loadFragment(frag1, "home");
}
private boolean loadFragment(Fragment fragment, String tagID){
if(fragment != null){
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
.replace(R.id.fragment_container, fragment, tagID)
.addToBackStack(null)
.commit();
return true;
}
return false;
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
String tagID = null;
switch(item.getItemId()){
case R.id.navigation_home:
Fragment cachedFragment1 = getSupportFragmentManager().findFragmentByTag("home");
if (cachedFragment1 != null){
fragment = cachedFragment1;
}
else{
fragment = new HomeFragement();
}
tagID = "home";
break;
case R.id.navigation_camera:
Fragment cachedFragment2 = getSupportFragmentManager().findFragmentByTag("cam");
if (cachedFragment2 != null){
fragment = cachedFragment2;
}
else{
fragment = new ReportFragment();
}
tagID = "cam";
break;
case R.id.navigation_map:
Fragment cachedFragment3 = getSupportFragmentManager().findFragmentByTag("map");
if (cachedFragment3 != null){
fragment = cachedFragment3;
}
else{
fragment = new MapFragement();
}
tagID = "map";
break;
}
return loadFragment(fragment, tagID);
}}
Working on an app that included 3 fragments. I want to retain data in the FragmentA when the user decides to switch back to FragmentA from FragmentB or FragmentC via BottomNavigationView.
My code works great with changing screen orientation, but for some reason it wont retain data on fragment change.
For testing, I've used only one EditText that should retain data from string.
MainActivity
public class MainActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener {
Fragment Frag = new Fragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this); // mOnNavigationItemSelectedListener
if (savedInstanceState != null)
{
Frag = getSupportFragmentManager().getFragment(savedInstanceState, "Frag");
}
else
loadFragment(new FragmentA());
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
int i = menuItem.getItemId();
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment)
{
if (fragment != null)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
Frag = fragment;
return true;
}
return false;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "Frag", Frag);
}
}
FragmentA
public class FragmentA extends Fragment {
EditText editText;
String string;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
string = savedInstanceState.getString("Text", "");
}
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.fragmentA, container, false);
editText = view.findViewById(R.id.centerText);
editText.setText(string);
return view;
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
string = editText.getText().toString();
outState.putString("Text", string);
}
}
I want that editText retains data when user switches back to FragmentA from FragmentB or FragmentC.
I've been struggling with this problem for some time now, trying different methods with no luck.
Either you can keep the references to your fragments, instead of creating new ones every time like you do here:
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
You could also use a ViewPager which allows you not only to swipe in-between the screens, but to set viewPager.setOffscreenPageLimit(i); which means that fragments will not be recreated unless they're too far off screen.
Here is your mistake
if (i == R.id.fragmentA) {
fragment = new FragmentA();
} else if (i == R.id.fragmentB) {
fragment = new FragmentB();
} else if (i == R.id.fragmentC) {
fragment = new FragmentC();
}
On click you create new fragment instance
Thank you for your replies.
This is what I changed. It is now working although I'm not sure if that's the elegant way to solve this problem. Data now retains in EditText while navigating back to FragmentA.
MainActivity
public class MainActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener {
Fragment Frag = new Fragment();
Fragment FragmentA;
Fragment FragmentB;
Fragment FragmentC;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this); // mOnNavigationItemSelectedListener
FragmentA = new FragmentA();
FragmentB = new FragmentB();
FragmentC = new FragmentC();
if (savedInstanceState != null)
{
Frag = getSupportFragmentManager().getFragment(savedInstanceState, "Frag");
}
else
loadFragment(FragmentA);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment fragment = null;
int i = menuItem.getItemId();
if (i == R.id.fragmentA) {
fragment = FragmentA;
} else if (i == R.id.fragmentB) {
fragment = FragmentB;
} else if (i == R.id.fragmentC) {
fragment = FragmentC;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment)
{
if (fragment != null)
{
getSupportFragmentManager().beginTransaction().hide(FragmentA).commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
getSupportFragmentManager().beginTransaction().show(fragment).commit();
Frag = fragment;
return true;
}
return false;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "Frag", Frag);
}
}
I'm currently using FragmentStatePagerAdapter and I would like it to have a "navigate up" feature (DisplayHomeAsUpEnabled). When I set the following code it shows up as a correct navigate back button. But nothing happens when I click on it, can you guys see anything that I', doing wrong in my code, I get no error while pressing the "navigate up" button and I get no compiler errors.
Here is the code, its in the "public boolean onOptionsItemSelected(MenuItem item) {" it gets interesting at the bottom.
public class ShareholdingDetailFragment extends FragmentActivity {
final int NUM_ITEMS = Portfolio.getPortfolio().count();
MyAdapter mAdapter;
ViewPager mPager;
Bundle extras;
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println(NUM_ITEMS + "e");
super.onCreate(savedInstanceState);
setContentView(R.layout.shareholdingdetailview_fragment_wrapper);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
public int getCount() {
return NUM_ITEMS;
}
public Fragment getItem(int position) {
return ShareholdingFragment.newInstance(position);
}
}
public static class ShareholdingFragment extends Fragment {
int mNum;
static ShareholdingFragment newInstance(int num) {
ShareholdingFragment f = new ShareholdingFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
#SuppressLint({ "NewApi", "NewApi" })
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shareholdingdetailview_fragment, container, false);
/****************Setting the Display as home and it shows*******************/
ActionBar bar = getActivity().getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
/**********************************/
return view;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home://Not working
System.out.println("test");//This isn't printed out
Intent upIntent = new Intent(getActivity(), DetailShareHoldingActivity.class);
if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)) {
getActivity().finish();
} else {
NavUtils.navigateUpTo(getActivity(), upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
Try adding: setHasOptionsMenu(true); in your onCreate() in ShareholdingFragment.
Add setHomeButtonEnabled(true) on API Level 14 and higher when you configure your action bar. On API Level 11-13, the home button is enabled automatically.
If you're using the new Toolbar and ActionbarDrawerToggle. You can assign clickHandler directly. For my activities that have this drawer toolbar I implemented an interface to enable drawer if at root,
#Override
public void enableDrawer(boolean enable) {
mDrawerToggle.setDrawerIndicatorEnabled(enable);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Pop fragment back stack or pass in a custom click handler from the fragment.
}
});
}