Saving action bar state - android

In my Android application I'm using the ActionBar. I want to show users some status using logo placed in the left side of the ActionBar. I can do this:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.new_icon);
and it works.
The problem is that this state in to persistent. If I change activity ActionBar reset itself, so default logo is shown. Is there any way to save state of the ActionBar during application runtime?

Use the standar lifecycle callbacks from Activities
In the OnCreate method of your activity try to read the state
public void onCreate(Bundle icicle) {
if (data!=null)
{
ActionBar actionBar = getSupportActionBar();
int state = data.getInt("status");
if (state==1)
actionBar.setIcon(R.drawable.icon_a);
else
actionBar.setIcon(R.drawable.icon_b);
}
}
And save the state to recovery it later
protected void onSaveInstanceState(Bundle data) {
super.onSaveInstanceState(data);
data.putInt(icon_state);
}

Related

getSupportActionBar setSubtitle only shows after activity recreation

I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.

ActionBar Back Button and Title

I have an activity that I would like the "up affordance" on.
public class ItemListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// ...
}
}
This adds the "back button" like I want. The problem is that the Title is not considered part of the back button in this instance. Is there any way to make it so that it behaves more like the Messaging app? In the Messaging app you can tap either the back button or the title and it will take you back to the previous activity.
Add an onClickListener to the title of your ActionBar. See this post for example: Set OnClick Listener on Action Bar Title in Android

why Actionbar title keep displaying the backstack title on hardware back button pressed?

I'm trying to change the title of activity, where i'm going to the activity from the fragments, so when i click on the hardware back button the title doesn't chnage to the one that i have provided in that activity i.e
this.setTitle("Something");
I have also tried
setTitle("Something");
Update
When i use the below mentioned code i run into a problem where the title that i assign in the mainActivity stay in all the pages, hence in the frgament getActivity().setTitle(""); seems to be useless in this case, so instead of changing everywhere the title using the below mentioned code i.e changing the actionbar title using getsupportactionbar.settitle(""), is there an easy way to do.
By any means can i know if an activity has came from the system back button back state or not?
You can handle this situation with onResume() method
//...
#Override
onResume(){
super.onResume();
this.getActionBar().setTitle("Something");
}
ı think, that will help you.
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
EDIT:
For SupportActionBar use:
ActionBar actionBar = getSupportActionBar();
EDIT2:
I use this to set Title from every fragment
#Override
public void onResume() {
super.onResume();
// Set the title
getActivity().getActionBar() //getSupportActionBar()
.setTitle(R.string.fragment_title);
}
If you always add your fragment to backstack when changing, then backpress can be override as follows to always get your previous fragment's title:
#Override
public void onBackPressed() {
int T= getFragmentManager().getBackStackEntryCount();
if (getFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
else if (getFragmentManager().getBackStackEntryCount() == 1) {
finish();
}
else {
String tr = getFragmentManager().getBackStackEntryAt(T-2).getName();
setTitle(tr);
getFragmentManager().popBackStack();
}
}
If you are occured this in Kotlin just remove Label from destination in Your navigation graph.
getSupportActionbar().setTitle("Your Title")
You have to call this whenever you want to change your title.
If you are switching between fragments call the method again and switch it back to your old title.

Start activity without ActionBar and add it in the onCreate event

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

setDisplayHomeAsUpEnabled(true) doesn't show arrow if called from non Activity class

In my Activity I want to show arrow to left of ActionBar icon, so in Activity I write:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
It works fine, but I decide move ActionBar initialization to another class and use it for all activities in my application, new class for this:
public class Utils {
public static void initActionBar(Activity activity, boolean homeIconNeeded) {
ActionBar actionBar = activity.getActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(homeIconNeeded);
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.action_bar_background));
}
public static void initActionBar(ActionBar actionBar, boolean homeIconNeeded) {
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(SmartVmsApplication.getContext().getResources().getDrawable(R.drawable.action_bar_background));
}
}
Then in my activity I insert in onCreate() callback initActionBar(this, true), however arrow doesn’t appear, no matters I passed Activity or ActionBar as parameter and it is an issue.
You forgot to call the setDisplayHomeAsUpEnabled() method.

Categories

Resources