Back Navigation in Android Without ActionBar - android

I have 3 screens, Screen 1 , Screen 2, Screen 3.
This is the kind of back navigation that I am implementing,
Screen 3 -> Screen 2 -> Screen 1.
Here is my below code for Screen 1
public class Screen1 extends Activity implements OnClickListener{
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
// This is where the intent starts to display the second Screen
case R.id.button1:
Intent started = new Intent(Screen1.this,Screen2.class);
startActivity(started);
break;
}
}
}
Screen 2
public class Screen2 extends Activity implements OnTouchListener{
ListView displaylist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// Please ignore this part of the code it just displays the list of items
displaylist = (ListView)findViewById(R.id.listView1);
String[] listofitems = {"A","B","C","W"};
ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);
displaylist.setAdapter(lists);
displaylist.setOnTouchListener(this);
}
// This is where the intent starts to display the Third Screen
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
return true;
}
}
Screen 3
public class Screen3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Displays some layout
}
}
My Current Problem
My current problem is that the back navigation(when I press back button) is in the following order
Screen3 -> Screen3 -> Screen2 -> Screen1
My Question
Why does the Screen3 display again when I press the back button ? Is this somekind of bug ?
From a similar question David Wasser comment says that
If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this
Testing Device
Android Emulator
API Level:19
Note: I do not want to do this with Actionbar

I think it's because your onTouch event is being fired twice, when pressing and releasing.
Try this, or use some other event:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Intent started2= new Intent(Screen2.this,Screen3.class);
startActivity(started2);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}

Related

Make screen not clickable until a button is clicked

I'm working on a kotlin app, My activity is in fullscreen and I want to make a button in which the screen can't be exit or back or anything unless the button is clicked. Like the one in the video player lock type.
My first idea was to make boolean isLocked and after clicking button change this value to the opposite and override all functions/events etc which You would like to lock and if isLocked is true just don't execute them.
Here is a simple code where I locked back button (in java but it can be easily changed to Kotlin)
public class MainActivity extends AppCompatActivity
{
boolean isLocked = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
isLocked ^= true;
System.out.println(isLocked);
}
});
}
#Override
public void onBackPressed()
{
if (!isLocked)
{
super.onBackPressed();
}
}
}
I think that in a similar way You can lock every event.
You can also make something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (!isLocked)
{
return super.onKeyDown(keyCode, event);
}
else
{
return true;
}
}
This will cancel every event like clicking back button or changing volume

Second onClick listener in a new activity

I just started to learn Java. I know some C++, but you know, I am just a novice. I have a problem with a button. I a main activity there are 3 buttons with onClick discovered by switch. By clicking on one of the buttons you're redirected to another activity where I need to create a new button.
The code responsible for MainScreen buttons looks like this (and it works):
public class MainScreen extends Activity implements View.OnClickListener {
Button act_2x2, act_3x3, act_4x4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
act_2x2 = (Button) findViewById(R.id.Activity_2x2);
act_3x3 = (Button) findViewById(R.id.Activity_3x3);
act_4x4 = (Button) findViewById(R.id.Activity_4x4);
act_2x2.setOnClickListener(this);
act_3x3.setOnClickListener(this);
act_4x4.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.Activity_2x2:
Intent inent1 = new Intent(this, macierz_2x2.class);
startActivity(inent1);
break;
case R.id.Activity_3x3:
Intent inent2 = new Intent(this, macierz_3x3.class);
startActivity(inent2);
break;
case R.id.Activity_4x4:
Intent inent3 = new Intent(this, macierz_4x4.class);
startActivity(inent3);
break;
}
And it is okay, I can normally enter the new activity, for example Activity_2x2.
Here, in 2x2 class I've created a new OnClickListener and when I click on it, nothing happens. I am sitting here for two hours with debugger, it is saying that I don't have permissions, but It is impossible, because it is just a simple button. I am using Android Studio and just don't know how to debug correctly.
Here is the definition:
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
Button b_2x2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2x2);
b_2x2 = (Button) findViewById(R.id.button_2x2);
b_2x2.setOnClickListener(this);
}
public void OnClick(View view) {
what happens after clicking
}
I know, that this problem is somewhere in overriding and extending, but no idea, why the compiller is letting this being compiled.
If someone have any idea, I will be grateful.
ps. I don't need an answer, just a point, what is wrong.
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
MainScreen already implements View.OnClickListener. Remove it from the definition of your class.
public class macierz_2x2 extends MainScreen {
is enough. You can override onClick on your macierz_2x2 activity
#Override
public void onClick(View view) {
switch(view.getId()) {
R.id.button_2x2:
// do something
break;
default:
super.onClick(view);
break;
}
}

Perform action only if two buttons are clicked

This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}

How to set window focus

In an android game app I have the splash screen layout with start, resume and exit buttons. From the start button I go to a surfaceview and start a worker thread from there.
I want to implement the device back button properly. When the user touches the back button, I come back to the splash screen menu. The problem is this: when I return to the splash screen, the start, resume and exit buttons don't react from user interactions anymore. It seems like they loosed focus.
How can I set the Splash Screen to have focus again?
public class SplashScreen extends Activity {
...
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_layout);
this.mRoadView = (RoadView) findViewById(R.id.road_view);
this.mStartButton = (Button) findViewById(R.id.start_button);
this.mResumeButton = (Button) findViewById(R.id.resume_button);
this.mExitButton = (Button) findViewById(R.id.exit_button);
prepareButtonListeners();
}
#Override
public void onBackPressed() {
if (mDoubleBackPressed) {
super.onBackPressed();
} else {
setContentView(R.layout.splash_screen_layout);
mDoubleBackPressed = true;
}
}
private void prepareButtonListeners() {
this.mStartButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mDoubleBackPressed = false;
setContentView(R.layout.road_view_layout);
}
});
...
...
...
Thank you very much,
Daniel.
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
view.dispatchTouchEvent(downEvent);
view.dispatchTouchEvent(upEvent);

Existing classes are not working in new project with intents

I wrote an app with 1 activity and 1 view, so it gives me the coordinates of my fingers i put on the screen. Worked perfectly.
The Problem is now: I Created a new App with more than 1 Activity so i can change between them with intents. also worked fine. But one Activity should be the one wich give me my finger positions. So i Copyed the class and activity put them into the manifest. And made a Button and a intend for to run it.
So when i try to run it it creates the class but doesnt react on my onTouchEvents anymore...
and i have no clue why. i hope i explained my problem well enough for u guys to understand.
So this is my main activity. starts the menu with the option to go to the not working class
public class V1_2 extends Activity implements OnClickListener{
Button btn_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_v1_2);
btn_1 = (Button) findViewById(R.id.button1);
btn_1.setOnClickListener(this);
}
public void onClick(View v) {
if( btn_1.getId() == ((Button)v).getId() ){
startActivity(new Intent(this,Obj_recog.class));
}
}
This is now the activity wich creates the touchpiont class for the touchevents
public class Obj_recog extends Activity implements OnClickListener{
touchtest TP;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blank);
TP = new touchtest(this);
}
public void onClick(View v) {
}
And now an example of what doesnt work here but worked at the last project the same way
public class touchtest extends View{
public touchtest(Context context) {
super(context);
Log.d("worked", "worked");
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
Log.d("Touch", "TOUCH!!!!");
return true;
}
}
So i get the message that it "worked" but it doesnt react on touchevents like it used to do...
It will be worked if you add touchtest view as your main view by using setcontentview and then
add touchlistener on that view and call your ontouchevent from your touchtest class. code will be like --
TP = new touchtest(this);
setContentView(TP);
TP.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TP.onTouchEvent(event);
return false;
}
});
i think should change this approach find an alternative way to implement for which you are going in this way.
In this way you can keep your blank layout and also TP.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
TP = new touchtest(this);
rl.addView(TP);
TP.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
TP.onTouchEvent(event);
return false;
}
});

Categories

Resources