I want Auto open drawable after start app android
used library https://github.com/mikepenz/MaterialDrawer
this code from Main Activity
If it possible
if possible
And the second question is, can I open the menu on all activities after the swipe to the right? Only the back button now works
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(mToolbar)
.withSelectedItem(1)
.withAccountHeader(headerResult)
.addDrawerItems(
new PrimaryDrawerItem().withIdentifier(1).withName(R.string.Home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withIdentifier(2).withName(R.string.News).withIcon(FontAwesome.Icon.faw_newspaper),
new PrimaryDrawerItem().withIdentifier(3).withName("About").withIcon(FontAwesome.Icon.faw_question_circle),
new PrimaryDrawerItem().withIdentifier(4).withName("Open Source").withIcon(FontAwesome.Icon.faw_github_square),
new PrimaryDrawerItem().withIdentifier(5).withName("Rate on Google Play").withIcon(FontAwesome.Icon.faw_thumbs_up)
)
.withTranslucentStatusBar(false)
.build();
drawer.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch (position) {
case 1:
drawer.closeDrawer();
return true;
case 2:
drawer.closeDrawer();
drawer.setSelection(1);
startActivity(new Intent(context, NewsListActivity.class));
return true;
case 3:
drawer.closeDrawer();
drawer.setSelection(1);
startActivity(new Intent(context, AboutTheDevActivity.class));
return true;
case 4:
drawer.closeDrawer();
drawer.setSelection(1);
libsBuilder.start(context);
default:
return true;
}
}
});
I suggest to refactor your application to single activity structure. This will help to solve both of your problems.
You may just open the drawer in activity onCreate method. It is called when activity created, with single activity - when app started.
Save that drawer was opened to instance state if you need more accuracy here and don't want the drawer to be opened on activity recreation events (states when system decides to destroy your activity to release memory)
use fragments for showing app screens
open drawer in this single activity and manage toolbar from activity so it could be opened on every screen
Single activity is the way google recommends to structure apps.
Related
In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.
You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.
Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.
Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks
Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.
For Kotlin write this in your MainActivity :
override fun onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
For Java write this in your MainActivity :
#Override
void onBackPressed() {
moveTaskToBack(true)
exitProcess(-1)
}
Hope to it will work for you as good as for me
hey i write some code for you
Variables:
boolean backactivity = true;
CODE:
public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}
I am working in android studio.I have a bottom-navigation fragment. For the last switch case, I need a button to navigate to the HomeActivity (home screen). I tried:
...
case SUMMARY:
navNextText.setText(R.string.end);
navNextImageView.setOnClickListener(v -> navigateHome());
navNextText.setOnClickListener(v -> navigateHome());
break;
default:
Log.w(TAG, "Executing a default case in navigateNext(). CTX: " + contextState.toString());
}
private void navigateHome() {
Objects.requireNonNull(getActivity()).finish();
Intent in = new Intent(getActivity(), HomeActivity.class);
startActivity(in);
}
This works to an extent. I can navigate home, but when I click back into the activity that the navigation fragment resides, the navigation bar resumes where it was previous to changing activity.
I have also tried adding onStop() and onDestroy to private void navigateHome, but that seemed to do nothing.
I am just wondering what is the cleaner way to close an activity from a fragment and start a new one.
just add finish() if you want to remove the current activity from the backstack
As you're in a fragment, use requireActivity().finish()
Also to make sure you have no activities in back stack, you can remove all using below intent flag
private void navigateHome() {
Objects.requireNonNull(getActivity()).finish();
Intent in = new Intent(getActivity(), HomeActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // <<<<< flag
startActivity(in);
requireActivity().finish(); // <<< finish the current activity
}
I have 3 activities: A,B,C. When I click on an item in RecyclerView in activity A activity B will be opened. There are a few String values which I have passed from A to B.
In activity B when I click on a button I go to activity C. In C when I press back button from my phone(android default back button) it comes back normally to activity B. But I have also put a back button in ToolBar using parent activity. When I press that button it shows me error that the String value trying to retrieve in activity B is null. I understand why the error is showing. It's obviously because there is no string passed from C to B to fetch.
But why am I not getting this error when I press back from my phones default button? How can I solve this?
Activity A
Intent intent = new Intent(getActivity(), Chat.class);
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", userName);
startActivity(intent);
Activity B
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
Activity C
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Getting an error on
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
because obviously nothing to fetch... how do i solve this
Add onClickListener to the back button of your toolbar:
toolbar.setNavigationOnCLickListener(new View.OnClickListener){
#Override
public void onClick(View view) {
finish();
}
});
If you are getting error for null then try this:
if(getIntent().getStringExtra("Recievers_Id") != null){
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
}
if(getIntent().getStringExtra("Recievers_Name") != null){
MessageRecieverName = getIntent().getStringExtra("Recievers_Name");
}
On activity B you should check is Intent has rquired Extra.
Intent i = getIntent();
if (i.hasExtra("Recievers_Id")) {
messageRecieverId = i.getStringExtra("Recievers_Id");
}
if (i.hasExtra("Recievers_Name")) {
messageRecieverName= i.getStringExtra("Recievers_Name");
}
You can set the working of back button on toolbar as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
First of all, you need to understand what happens when user presses navigation up button and what happens when onBackPressed.
Based on the documentation:
If the parent activity has launch mode <singleTop>, or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought
to the top of the stack, and receives the intent through its
onNewIntent() method.
If the parent activity has launch mode , and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP, the parent activity
is popped off the stack, and a new instance of that activity is
created on top of the stack to receive the intent.
Where as when back button is pressed, it navigate, in reverse chronological order, through the history of screens. In this case Activity won't be re-created.
To summarize, the lauch mode of your ActivityB in AndroidManifest.xml must be standard. In this case the activity will be recreated when you press the navigation up button. But when back button is pressed, ActivityB is just brought to front, hence the difference in behavior.
Solution:
Approach 1: handle Up button by yourself
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Approach 2:
Set launch mode of Activity B to singleTop in AndroidManifest.xml. Remember to check the implication of changing the launch mode.
The "standard" and "singleTop" modes differ from each other in just
one respect: Every time there's a new intent for a "standard"
activity, a new instance of the class is created to respond to that
intent. Each instance handles a single intent. Similarly, a new
instance of a "singleTop" activity may also be created to handle a new
intent. However, if the target task already has an existing instance
of the activity at the top of its stack, that instance will receive
the new intent (in an onNewIntent() call); a new instance is not
created.
How can I prevent the ActionBar back button (we gonna say ABBB) of my SecondActivity to recreate the MainActivitywhen clicked ?
I have a ListView in the MainActivity and it can be edited using the SecondActivity. The problem is that when the user presses the ABBB, the ListView is reset to default...
However, when the user clicks my OK button or presses physical back button, there is not any problem because I'm using finish();
#Override
public void onBackPressed() {
finish();
}
If I use this code... :
switch (item.getItemId()) {
case (android.R.id.home):
finish();
}
...there is the same problem because, I guess, this method is called after "Android's code".
How can I totally override Android ABBB's clicked code to set it to just call finish(); ?
The ideal scenario is, when are you in the SecondActivity (I take it that, this means that you are in Edit mode), and you press the device back button or ABBB, you show a subtle alert to the user saying "do they really want to dismiss the editing", or go ahead and apply the edit done as in the Contacts application.
So that being said, if all you require is to finish() the activity on press of ABBB, the code that you shown above should work fine (though you need to put return true; after finish()). It works for me. Try the below one in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed(); // or finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I put onBackPressed(); because your ABBB with now imitate your device back button.
Or, you can set your parent activity's launch mode as -
android:launchMode="singleTop"
that will do all without changing anything.
Reference
I am following this link to implement actionbar.
i have modified this code like this
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition] , Toast.LENGTH_SHORT).show();
if(actions[itemPosition] == "Bookmark")
startActivity(new Intent(this,Bookmark.class))
else if(actions[itemPosition] == "Subscribe")
startActivity(new Intent(this,Subscribe.class))
else
startActivity(new Intent(this,Share.class))
return false;
}
};
When i run this code it opens Bookmark activity . but it call over main layout like when it opens bookmark and i click back then it opens main activity. i want when i click on list for e.g bookmark then only the activity should change not the title with list navigation. or is there any example that i should follow to call new activity when list navigation is called. i have also tried ActionBarSherlock it also change the textview on clicking list navigation not calling new activity
Try This instead of above
if(actions[itemPosition].compareTo("Bookmark")==0){
startActivity(new Intent(this,Bookmark.class));
}
else if(actions[itemPosition].compareTo("Subscribe")==0){
startActivity(new Intent(this,Subscribe.class));
}
else{
startActivity(new Intent(this,Share.class));
return false;
}
You cannot open new activity on current activity. That's not how it works. If you want to change the views within your activity when the item is clicked you can, and should look into fragments to do so.