• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Prevent/hide back button in new Activity

superx335

Lurker
Dec 1, 2014
5
0
Hello everybody,
I am so happy for being in here, I am a web developer that is just starting with Android because a business asked me to develop a simple app to avoid papers when their technicians are going to repair computers, TPV, etc.
So I am supposed to develop an Android app that is kinda HTML form, and some options to list the tasks and to send them to OpenERP (this wil be the last part and it doesn't matter at the moment
smile.png
)

Surprisingly, the major problems I am having are related to UI options, I would like to understand better the LinearLayout I am working on it, but at the moment the main problem I've found is that when I create the new Activity after a succesful login (because Activities are the natural way in Android to have a diferent "page", isn't it?) I show the main menu, but in the top bar (Navigation bar) you always see the back button. If I use "finish()" or delete the history by using flags, you are still able to press that button (and of course of seeing it), but exiting the app instead of going back to the login activity.
What I would like is to hide that option and, maybe, I will enable a logout button, but at the moment I would like to hide the posibility of going back.

By the way, I will posibly use SQLite in order to get offline information and send it to the ERP, but I have been told that Android allows temporal savings just in case you press "home" or use another applications, but even when I didn't implement anything, it already seems to do it, because I can write something in the login Activity, press home, navigate the Internet, and when I go back to my App the written information is there, should I do something or it is just "automatic"?.
Do you think I am crazy for accepting this "job"? I feel so motivated!.

Thank you in advance for any help!!
 
I'm not that good myself at Android but you can make back button not work by simply overriding:
Code:
@Override
public void onBackPressed()
{
}

I don't know if you can hide it or not or if my solution is optimal for that.


As for it saving the data automatically when you press Home - no, it's not auto. If the device will be low on memory your application will Stop or Destroy itself so going back to it will start it from scratch. Fortunately there's a way to save data when this happens, overriding the OnSaveInstanceState method:
Code:
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
  // save application stuff
  savedInstanceState.putByte("some_byte_data", some_byte);
  savedInstanceState.putFloatArray("some_data", float_array);

  // call superclass method so it can save the view hierarchy state
  super.onSaveInstanceState(savedInstanceState);
}

And then in OnCreate you check if the passed bundle is null or not, if it's not it means the app is restarting from a previous state.
Code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  // initialize some usual stuff

  // if we're restarting from a previous state
  if (savedInstanceState != null)
  {
    // load values
    byte some_byte = savedInstanceState.getByte ("some_byte_data");
    float[] float_array = savedInstanceState.getFloatArray("some_data");

    // initialize the app according to the values read above, so that it looks like it restarted flawlessly
  }
}
 
Upvote 0
Wow man that's a very good info. I am just too newbie to know if "savedInstanceState.putByte("some_byte_data", some_byte);" is, for example, the content of an EditText (since it is gonna be mainly a formulary) or it should be another kind of parameters.

If it would be any thing, I could just load data by "byte some_byte = savedInstanceState.getByte ("some_byte_data");" and place it inside the correct EditText.

Maybe I am thinking too much in JavaScript way, but is the only thing that makes sense for me :D

And, I guess, I can leave SQLite just to manage the information once they are finished and correctly stored, ready to send to the server when the user press the button, for example.

Thank you very much :)
 
Upvote 0
If you don't know what a byte, short, long, float etc is, I suggest you google them up and read in some Java documentary, it's going to be extremely useful in your future and I don't really want to explain it all here.

But for text you use String so putString and getString. Do note that I recommend checking the official Android documentation on Bundle as well if you're confused or don't get it, particularly the "key" string in the parameter of those functions; it's basically the "name" you give to the saved variable.

For views I'm not entirely sure if it gets automatically saved by calling the super.onSaveInstanceState(savedInstanceState) so you should try it out first if it works just like that. An easy way to test application restarts is to not have the orientation flag in the android:configChanges= value for the activity, in your Manifest file. And then just rotate the screen of your device and the app will restart - and notice the results, if the state you had was saved properly or not.

Don't forget to put that orientation flag back if you designed it that way.
 
  • Like
Reactions: superx335
Upvote 0
Thank you very much Manaya,

sorry because I didn't explain myself in a proper way. I do know what a byte, short and long are, hehe, I have been programing in C, C++, and even in Java (not enough, as you can see) but I feel so unsafe while programming in Android since I've been developing web applications for a long time and with the magic of php I almost forgot even how to declare a variable :D

I get the purpose of the key's, I will have to check out if I need to do it almost manually, kinda php, getting every content of the EditTexts and saving it to get it back when restoring.

Anyway I like the last part you told me, I hope that is possible and I can save a whole instance of the view. And that rotation tip is just awesome.

Thanks a million again!!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones