I have implemented a navigation slider in my app. In this slider I have four fragments: Home, Profile, Search, calculator. Don't worry about the naming conventions in selectItem method as I am only focusing on searchProgram as of now and don't have all my four fragments implemented
NavigationDrawer.class (Activity):
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
public void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomePage();
break;
case 1:
fragment = new HomeFragment();
break;
case 2:
fragment = new SearchProgram();
default:
break;
}
if (fragment != null) {
Bundle bundle = new Bundle();
bundle.putInt(HomeFragment.EXTRA_ITEM_INDEX, position);
bundle.putString("username", userName);
bundle.putString("access_token", access_token);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
When I select search i am brought to a view which has an action bar with the navigation slider just like I wanted. When the user selects what they want to search for from the two spinners and clicks my own button called search I then start a master detail flow containing an expandable list.
Below is where i start my new activity from my searchProgram class(Fragment):
protected void onPostExecute(ArrayList<SportProgram> programList) {
Bundle extras = new Bundle();
String userName = getArguments().getString("username");
String access_Token = getArguments().getString("accessToken");
System.out.println("Search Program Test Username: " + userName);
System.out.println("Search Program Test Access_Token: " + access_Token);
extras.putString("username", userName);
extras.putString("accessToken", access_Token);
extras.putParcelableArrayList("programs", programList);
Intent intent = new Intent(getActivity(), TrainingProgramListActivity.class);
intent.putExtras(extras);
startActivity(intent);
}
}
This view has an action bar with a back/up button and no navigation slider. When I click this button I am brought back to the previous view, i.e. the search view, however the action bar stays the same and still has the up button. Has anyone any idea on how to change the action bar to include the navigation slider like it did previously? also when I select the back/up button in my listActivity view I am replacing the current fragment as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list_program, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (item.getItemId()) {
//This is the back/up button
case android.R.id.home:
fragment = new SearchProgram();
break;
case R.id.action_home:
Intent homeIntent = new Intent(this, NavigationDrawer.class);
Bundle extras = this.getIntent().getExtras();
homeIntent.putExtras(extras);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
System.out.println("HIT HOMEINTENT");
return true;
case R.id.action_search:
fragment = new SearchProgram();
System.out.println("HIT SEARCH");
break;
default:
break;
}
if (fragment != null) {
Bundle bundle = this.getIntent().getExtras();//new Bundle();
// bundle.putString("username", userName);
// bundle.putString("access_token", access_token);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.containerE, fragment).commit();
// fragment.getActivity().supportInvalidateOptionsMenu();
}
return super.onOptionsItemSelected(item);
}
I have tried numerous possible solutions but keep falling short. All the examples i have found navigate from the navigationDrawer activity to a fragment and then another fragment but for me I am navigating from NavigationDrawer to a fragment->class->fragment. Any ideas please?
You are stacking a detail activity on top of the main one which holds your drawer. To get back to the original activity, just close the last one using finish(), in your onOptionsItemSelected().
Related
I have an application android where to start step one bundle.putString ("statusLogin", "unlogged"); referring to my state of login, and so some features hidden buttons.
In my menu item an option "login as admin" that after the login has been carried out step a new parameter for the bundle: bundle.putString ("statusLogin", "Logged") and so I set visible the buttons allowed only to administrator .
Until then fine, but that in menu item possess the button "Init" where I would need to seek the status of Bundle without creating a new one so i can verify that it is logged in or not and show buttons allowed.
This is my main activity
public class PrincipalActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
FragmentManager fragmentManager = getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString("statusLogin", "Unlogged");
TelaInicialFragment fragobj = new TelaInicialFragment();
fragobj.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.principal, fragobj).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_principal, menu);
return super.onCreateOptionsMenu(menu);
}
public static PrincipalActivity newInstance(Context context) {
PrincipalActivity principalActivity = new PrincipalActivity();
return principalActivity;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
int itemId = item.getItemId();
switch (itemId) {
case R.id.init:
Bundle bundle = new Bundle();
bundle.putString("statusLogin", "Unlogged");
TelaInicialFragment fragobj = new TelaInicialFragment();
fragobj.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.principal, fragobj).commit();
break;
case R.id.sincronizar:
break;
case R.id.logarAdmin:
fragmentManager.beginTransaction().replace(R.id.principal, new LoginFragment()).commit();
break;
case R.id.sair:
finish();
break;
default:
return false;
}
return true;
}
}
How can I check the Bundle state to ensure that the user is currently authenticated?
I have some problem with Fragment and addToBackStack. I am create some project using Activity, Fragment, ActionBar, navigation spinner. Activity (MainActivity) is parent with two fragment (BestFragment and CurrFragment). BestFragment using action bar with navigation spinner to call two data (Buy and Sell) and display in Listview (default load is Buy data). When listview (BestFragment) setonitemclicklistener then calling detail selected item and display to CurrFragment. I am using addToBackStack from BestFragment to CurrFragment. When i am choose Sell data from navigation spinner (BestFragment) and selected item in Listview, it will be send detail on CurrFragment. If i am press back button in action bar (CurrFragment), that's can be back to BestFragment but that's load default Buy data (not Sell data on previous BestFragment). Help me.
Main Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case android.R.id.home:
getFragmentManager().popBackStack();
getActionBar().setTitle(mTitle);
//setTitle(navMenuTitles[1]);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
BestFragment
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int p,
long arg3) {
// TODO Auto-generated method stub
//ambil mata uang
String des = bestList.get(p).getMataUang();
//ambil kata terakhir textview
String a[] = textinfobest.getText().toString().split(" ");
String last_text = a[a.length-1];
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
CurrFragment llf = new CurrFragment();
Bundle bundle = new Bundle();
bundle.putString("tambah title", des +" "+last_text);
bundle.putString("pilih",des+last_text);
llf.setArguments(bundle);
ft.replace(R.id.frame_container, llf);
ft.addToBackStack(null);
ft.commit();
}
});
I have problem with the back button in action bar to fragment
my code fragment:
public class Server extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_server, container, false);
Button server = (Button) view.findViewById(R.id.status);
/** Button Check Status Server **/
server.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(view.getContext(), ServerStatus.class);
startActivityForResult(myIntent, 0);
getActivity().finish();
}
});
return view;
}
}
my code in activity:
public class ServerStatus extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_status);
}
}
My code in Fragment
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment objFragment = null;
switch (position) {
case 0:
objFragment = new Account();
break;
case 1:
objFragment = new AllNews();
break;
case 2:
objFragment = new Server();
break;
case 3:
objFragment = new Account();
break;
case 4:
objFragment = new Account();
break;
case 5:
objFragment = new Account();
break;
case 6:
objFragment = new About();
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container,objFragment)
.commit();
}
Every time I click the back button, the program always closes. I already tried to use:
getActionBar().setDisplayHomeAsUpEnabled(true);
and this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Can anyone help me to fix my program?
EDIT: Concerning cross-activity-navigation, you should not use startActivityForResult() in that way. Try to use startActivity() instead. If you start an activity for a result, the calling activity waits for the onActivityResult() callback and should not be finished.
In the manifest file you should declare the fragment's activity as parent activity of ServerStatus to enable back navigation. You should not need NavUtils.
If you want to enable fragment navigation within an activity, you have to add your fragment transactions to the backstack:
getFragmentManager().beginTransaction().addToBackStack(null).replace(...).commit();
Then you have to call getFragmentManager().popBackStack() in onOptionsItemSelected() to enable back navigation for the action bar:
#override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
getFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
You may have to override onBackPressed() to enable back navigation for the back button:
#override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() >= 1) {
getFragmentManager().popBackStack(); // return to previous fragment
}
else {
super.onBackPressed(); // Exit application when no fragment is on the backstack
}
}
I am using the navigation drawer in Android Studio. When I select an item in navigation drawer I'm using the following code:
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
When I call my activities from the navigation drawer item selected the action bar disappears and activities open on full screen. How can I manage that the navigation drawer doesn't disappear?
If you want to persist navigation drawer you should change content fragment, instead of showing Activity.
In your case it would be, change
Intent intent1 = new Intent(MainActivity.this,HomeActivity.class); startActivity(intent1);
with:
FragmentManager fragmentManager = ...
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.your_fragment_container_id, new HomeFragment())
transaction.commit();
dont use DayActivity_1.class activity use fragment instead and when you click on navigation item jst create fragment and replace it to the drawerlayout
Extend FragmentActivity in you main navigation activity class like below.
public class NavigationdrawerActivity extends FragmentActivity
Now use the below code to solve your problem
public void setContent(Fragment fragment) {
// Fragment fragment = new content_home();
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
fragmentManager.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_left);
// fragmentManager.beginTransaction()
fragmentManager.replace(R.id.mainContent, fragment).commit();
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,DayActivity_1.class);
startActivity(intent2);
break;
}
}
public void onNavigationDrawerItemSelected(int position) {
switch(position)
{
case 0:
Fragment homeActivityFragment = new HomeActivityFragment();
//if you want to pass data to fragment
//Bundle bundle = new Bundle();
//bundle.putString("id", "" + item.get("id"));
//homeActivityFragment.setArguments(bundle);
setContent(homeActivityFragment)
break;
case 1:
Fragment dayActivity_1Fragment = new DayActivity_1Fragment();
setContent(dayActivity_1Fragment)
break;
}
}
I'm using this tutorial to implement facebook login etc.
Facebook Login
I have added new fragment in this to show list of friends. Now when I press back button on my newly added fragment it takes me to the SPLASH fragment, I want same behaviour on back button on action bar. Means when I'm on my new fragment it shows me a back button on action bar. And pressing that back button takes me back to the SPLASH screen.
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
You can do that by two ways :
1. Inside your fragment
#Override
public void onDetach()
{
super.onDetach();
PUT YOUR CODE HERE
}
This will called when fragment will be finished.
2. Just add addToBackStack while you are transitioning between your fragments like below:
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("tag").commit();
if you write addToBackStack(null) , it will handle it by itself but if you give a tag , you should handle it manually.
EDITED:
for doing transactions
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.replace(R.id.content_container, scheduleFragment, "scheduleFragment");
fragmentTransaction.addToBackStack("scheduleFragment");
fragmentTransaction.commit();
#Yawar actionbar is on activity only and this will added on activity it will be called evertime when u press actionbar home button-->
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i got this code on stackoverflow after searching hard hope this may help you
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// in on Create()
search for code onOptionsItemSelected(MenuItem item)
and edit it in this way
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// change your behaviour here
Intent intent = new Intent(this, yourclass.class);// i started new activity here
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}