I'm creating an application to which I've added a Botton Navigation View but I don't know if there is any way that it appears in all the Activity or if on the contrary I would have to specify it in each of them to appear, as well as adding all its functionality on each screen, since this last option I see a little cumbersome.
The other question is for the button Home of this bar, I have added a button that is home to return to the main Activity each time I pulse, in the way that I have done has been with an Intent that goes to that screen itself, but I do not know if there is another method:
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.btnHome:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
Check out how to use Fragment in Android. You don't have to open a new activity each time the button in your BottomNavigationView is clicked, instead, you use fragments, so only the selected part in activity is changed. Check out the following tutorial to learn more about Fragments. See the "dynamic creation" of fragments.
How to use Fragments in Android.
In your case, there'll be code of FragmentTransaction where you're launching a new Activity using Intent. You'll be replacing only a certain portion of the screen(between your Toolbar and `BottomNavigationView).
Related
There is a NavigatorView in which I am using the NavController. The whole problem is that when replacing the Fragmenta, I immediately set fragmentTransaction.addToBackStack (String.valueOf (FragmentRink3)); and when navigating to another NavController when clicking on OnBackPressed, is applied to the fragment from the previous NavController where the replacement was made. It's hard to explain. Is it possible to somehow do so that each of the lower navigation menu has its own, I don’t know, a list of clicks, according to which the sequence of transitions to fragments was determined? I know I explained very poorly, I will attach a gif to show the problem clearly.
As you can see, by clicking on the button, the fragment was replaced, then I went to the next menu and clicked OnBackPressed, and at this moment the fragment, which was replaced in the previous menu, went back. How to fix it?
I will replace the fragment as follows:
btn_close.setOnClickListener(v ->
{
FragmentRink4 fragment = new FragmentRink4(); // you fragment
FragmentManager fragmentManager = ((FragmentActivity) v.getContext()).getSupportFragmentManager(); // instantiate your view context
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.nav_controller_rink1, fragment);// your container and your fragment
fragmentTransaction.addToBackStack(String.valueOf(FragmentRink3));
fragmentTransaction.commit();
});
Here is a link to the project.
In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
In the main activity of my app there is a container that hosts fragments.
When a user clicks a button in the "default" fragment (the first fragment that is displayed), the fragment changes, and so do the actionbar buttons.
One of the buttons in the actionbar of this new fragment open another activity.
In that activity, when a user clicks the back button, the activity closes, and the fragment that was shown in the MainActivity (the fragment that opened the new activity) is still there (which is fine).
However, if a user clicks the back button again, it does not return to the previous fragment. While it does return when the activity does not open.
It turns out that opening the activity clears the backstack (verified by Logging the count from the FragmentManager class), while I'm not quite sure whether this is supposed to behave like this or not, it kinda makes sense. Unfortunately, it is not the behavior I desire.
MainActivity: Fragment A (default) ---> Fragment B ---> Acivity B
Therefore, my question is how can I keep the backstack after the activity resumes, if at all?
I tried searching for similar questions, but all questions I found actually asked how to clear the backstack.
Try that:
#Override
public void onBackPressed() {
Intent intent = new Intent(A_Acticity.this, B_Activity.class);
startActivity(intent);
}
Hope it helped! :)
Reading the documentation, there is a way to pop the back stack based on either the transaction name or the id provided by commit. Using the name may be easier since it shouldn't require keeping track of a number that may change and reinforces the "unique back stack entry" logic.
Since you want only one back stack entry per Fragment, make the back state name the Fragment's class name (via getClass().getName()). Then when replacing a Fragment, use the popBackStackImmediate() method. If it returns true, it means there is an instance of the Fragment in the back stack. If not, actually execute the Fragment replacement logic.
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
I recently started coding in Eclipse and I haven't done much yet so this is more or less my first app. I'm trying to make my school scheudele, it's simple: first activity shows 5 buttons, each button leading to a new activity (monday - friday).
How would I make so that when I click a certain button a new activity (let's say monday) would pop up?
I've seen hundreds of these questions already asked and answered on here but I just don't get it. It's useless to copy & paste code from here if I still don't get what's going on. I know I have to create a new intent and buttonlistener but I just don't get it what for and what to do then.
Could someone explain it to me as detailed as you can how exactly switching between activities using buttons work and how to actually do it?
I have:
MainActivity.java
Monday.java
5 buttons (button1-5)
So how would I code button1 to switch from MainActivity.java to Monday.java?
Start by adding android:onClick="onClick" to each of your buttons' XML elements. This will make your buttons execute the onClick method whenever an onClick event is triggered on them.
Then in your MainActivity class add the following method:
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// Monday
Intent intent = new Intent(MainActivity.this, Monday.class);
startActivity(intent);
break;
case R.id.button2:
// Tuesday
Intent intent = new Intent(MainActivity.this, Tuesday.class);
startActivity(intent);
break;
// the rest of the buttons go here
default: Log.e("YourTAG", "Default in onClick hit!");
break;
}
}
So every time there is an onClick event on any of your five buttons, the onClick method above will execute with the argument representing the View you just clicked on.
Details regarging intents and how they work here
And as #Edward noted, don't forget to add your new activities in your AndroidManifest.xml file under the application element, such as:
<activity android:name=".Monday" android:label="#string/app_name"></activity>
what you should do is to create an Intent that will fire your Activity you do that by this code:
startActivity(new Intent(YourCurrentActivity.this, Monday.class));
You will have to fire a different Intent on each button that will create the appropriate Activity.
Of course don't forget to declare your Activitys in your manifest file.
OK. Lets say you have following button in your layout file:-
<Button
android:id="#+id/my_btn"
android:layout_width="55dp"
android:layout_height="22dp"
android:onClick="goToMonday" // function name which will be in MainActivity.java
/>
Now in your MainActivity.java:-
public void goToMonday(View v){
Intent monday_intent = new Intent(MainActivity.this, Monday.class);
MainActivity.this.startActivity(monday_intent);
}
This is the way to change activities.
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.