i writed an application which contains an option button and Option Activity.
In the OptionActivity, you can choose play a music "on" or "off" with togglebutton. and there are some check button also.
until here everything is okey but when you check some box and back to the main menu and back to the Options Activity again, you find that all variables is default value. for example the checked box one is uncheckhed. I know that it is because of everyCall the Options Activity with new Intent but i want to know that starting an activity "without using new Intent"
This is my SPLASH SCREEN
public class MainActivity extends Activity {
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baslangic);
Thread thred=new Thread(){
public void run(){
try {
sleep(5000);
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OPTIONS"));
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
finish();
}
}
};
thred.start();
}
}
This is my MAIN ACTÄ°VTY which contains options button
public class Baslangic extends Activity {
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start=(Button)findViewById(R.id.button1);
Button options=(Button)findViewById(R.id.button2);
Button about=(Button)findViewById(R.id.button3);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OYUN"));
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.ABOUT"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And This is My options Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
it=(RadioButton)findViewById(R.id.radio0);
ti=(RadioButton)findViewById(R.id.radio1);
final ToggleButton but=(ToggleButton)findViewById(R.id.toggleButton1);
music=MediaPlayer.create(Options.this, R.raw.avril);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(but.isChecked())
music.start();
else
music.pause();
}
});
it.setChecked(true);
Button menu=(Button)findViewById(R.id.button1);
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
}
}
NOTE: Be aware That when the user click the options Button, i call finish(), actually i have a solution. My solution that, after the splash screen, i start 2 activity; first Options later Menu, And when the user click the Options button and i call the finish() so i am in the options Button :D and when press back i called new Intent(menu) you can see on OPTIONS activity,
So my problem is already solved but i want to know that there should be a solution different from this way, If you know pls help me. :)
Are you using the PreferenceActivity or a normal Activity superclass?
Anyway, be sure to read about the SharedPreferences - it can store your values for entire app or for single activity.
Related
I'm having an Issue that I have two Activities A and B when I clicked on Button to change an Activity from A to B . It Restart my Application I don't know what going wrong Please help me
public class Login_Choice_Activity extends AppCompatActivity{
private Button d_btn,p_btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login__choice);
FindAllView();
p_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new
Intent(Login_Choice_Activity.this,Patient_SignIn_Activity.class));
Toast.makeText(getApplicationContext(),"CLICKED",Toast.LENGTH_LONG).show();
}
});
}
private void FindAllView(){
d_btn = findViewById(R.id.choice_doctor_btn);
p_btn = findViewById(R.id.choice_patient_btn);
}
}
https://i.stack.imgur.com/KcgAh.gif
It could be better if you create a method for opening the new Activity and call it from onClick(View v) method. Example
private void openActivity() {
startActivity(new Intent(this,Patient_SignIn_Activity.class));
}
In your onClick call:
#Override
public void onClick(View v) {
openActivity()
}
Below is the code which I use.By this code the background music plays on all activities.But when I come back to this activity using home button then music plays normally but the drawables will not resumed. That Mean if music is played and I went back to that activity via home button then play button is shown rather than the pause button but I want that if music played on resume then button is pause and if not then show play button.
Can you tell what to do with play and pause buttons on resuming activties?
public class RadioActivity extends ActionBarActivity implements RadioListener{
private final String[] RADIO_URL = {"url "};
private ActionMenuView amvMenu;
ImageButton mButtonControlStart;
TextView mTextViewControl;
RadioManager mRadioManager = RadioManager.with(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
mButtonControlStart = (ImageButton) findViewById(R.id.buttonControlStart);
mRadioManager.registerListener(this);
mRadioManager.setLogging(true);
initializeUI();
mButtonControlStart.setBackgroundResource(R.drawable.play);
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar);
toolbarBottom.setTitle("Radio");
amvMenu = (ActionMenuView) toolbarBottom.findViewById(R.id.amvMenu);
amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
// toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.activity_aboutus:
// TODO
Intent intent=new Intent(RadioActivity.this,Contact_us.class);
startActivity(intent);
break;
// TODO: Other cases
case R.id.menu_refresh:
// TODO
Intent intent1=new Intent(RadioActivity.this,Web.class);
startActivity(intent1);
break;
case R.id.menu_about:
Intent intent2=new Intent(RadioActivity.this,Song_rqst.class);
startActivity(intent2);
break;
case R.id.menu_item_share:
Intent intent3=new Intent(RadioActivity.this,Ratee.class);
startActivity(intent3);
break;
}
return true;
}
});
}
public void initializeUI() {
mButtonControlStart = (ImageButton) findViewById(R.id.buttonControlStart);
mTextViewControl = (TextView) findViewById(R.id.textviewControl);
mButtonControlStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mRadioManager.isPlaying()) {
mRadioManager.startRadio(RADIO_URL[0]);
mButtonControlStart.setBackgroundResource(R.drawable.end);
}
else {
mRadioManager.stopRadio();
mButtonControlStart.setBackgroundResource(R.drawable.play);
}
}
});
}
#Override
protected void onResume() {
super.onResume();
mRadioManager.connect();
}
#Override
protected void onDestroy() {
super.onDestroy();
mRadioManager.disconnect();
}
#Override
public void onRadioLoading() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//TODO Do UI works here.
mTextViewControl.setText("RADIO STATE : LOADING...");
}
});
}
#Override
public void onRadioConnected() {
}
#Override
public void onRadioStarted() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//TODO Do UI works here.
mTextViewControl.setText("RADIO STATE : PLAYING...");
}
});
}
#Override
public void onRadioStopped() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//TODO Do UI works here
mTextViewControl.setText("RADIO STATE : STOPPED.");
}
});
}
#Override
public void onMetaDataReceived(String s, String s1) {
//TODO Check metadata values. Singer name, song name or whatever you have.
}
#Override
public void onError() {
}
Whenever you come back to your Music Play Activity Oncreate() is not called so your drawable resource load on Onpause() or Onstart(), Everytime Oncreate() is not calling.
How to keep the button activity on background/pause or something like this when i press the physical back button . I want this activity to set on when i press again the button.(to show me what did i paint in last activity);
MainActivity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonSave=(Button)findViewById(R.id.buttonDraw);
buttonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,ButtonDraw.class));
}
});
}
ButtonDraw :
public class ButtonDraw extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(com.example.myfirstapp.R.layout.paintingfile);
BlackPixel blackPixel;
blackPixel = new BlackPixel(this);
setContentView(blackPixel);
blackPixel.requestFocus();
}
}
In your MainActivity include this method:-
#Override
public void onBackPressed() {
startActivity(new Intent(this,ButtonDraw.class));
finish(); // this line depends on you whether you want to finish the MainActivity or not.
}
The above method gives you control over the back button of android.
I need the code to show and hide the notification bar during an activity.
I try this code:
public void goFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
public void goNonFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
This doesn't work after the content is adding...
Remove the following line from Android Manifest file.
android:theme="#android:style/Theme.Black.NoTitleBar" and check .
Hope it will help you.
This is the code i have used to check it .
public class Main extends Activity {
Button magic,button1,button2;
TextView display;
int random;
private Long startTime=System.currentTimeMillis();
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ssss);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
goFullscreen();
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
goNonFullscreen();
}
});
}
public void goFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
public void goNonFullscreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.findViewById(android.R.id.content).requestLayout();
}
I have 2 activities. One called Login and another called ListDeals. Login has a form with a submit button which, when pressed, should switch our activity to ListDeals. This works.
However, in Logcat I don't get a message saying that the activity has started. Also, when in ListDeals, if the user presses the back button, both activities should be killed. I looked around and this is what I came up with:
public class Login extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
Button submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(onSubmit);
}
private View.OnClickListener onSubmit=new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent=new Intent(view.getContext(),ListDeals.class );
startActivityForResult(myIntent,0);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login_screen, menu);
return true;
}
#Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
if(resultCode==2) {
finish();
}
}
}
public class ListDeals extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deals_list);
System.out.println("starting activity");
}
#Override
protected void onDestroy() {
System.out.println("dude");
setResult(2);
super.onDestroy();
}
protected void onStop(){
setResult(2);
super.onStop();
}
}
However, when I press the back button, It takes me back to the Login activity which is not what I want.
In your click handler, onSubmit, just finish your login activity, like this. That way, it won't be on the activity stack, and as a result, pressing back from your other activity will bring the user back to where they started.
private View.OnClickListener onSubmit=new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent myIntent=new Intent(view.getContext(),ListDeals.class);
startActivity(myIntent);
finish();
}
};
Also, to address you second question, use the Log class methods, such as Log.i or Log.d for debug messages, those will show up in logcat.
Finish Login before starting the new Activity. Besides you don't need the result.
private View.OnClickListener onSubmit=new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent=new Intent(Login.this, ListDeals.class );
startActivity(myIntent);
Login.this.finish() // add this to finish it.
}
};