Activity not stopping when i press back button - android

In my project, when i click on the button in the first Activity it goes to a second activity. Second Activity plays a video. Video works fine, but when i the press back button on phone it goes back to the first Activity but video sound is still on, meaning video is still playing. How can i stop it when i press back button or click back ?
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(), gnrl.class);
startActivity(int0);
}
#SuppressLint("CutPasteId") public class gnrl extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matematik);
String[] myArray= getResources().getStringArray(R.array.Matematik1);
ArrayAdapter<String> aad= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray);
setListAdapter(aad);
ListView listview=(ListView)findViewById(R.id.listmat);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(),Test.class);
startActivity(int0);
}
}
});
}
private void setListAdapter(ArrayAdapter<String> aad) {
// TODO Auto-generated method stub
}
}

Try overriding the onPause method of the second activity and stop the video playback there.

Just call finish() when you leave the activity playing the video.
protected void onPause() {
finish();
}
Or if you want to leave the activity after pressing a button, do something like
Button goBackButton = (Button) view.findViewById(R.id.back_button);
edit.setOnClickListener(new OnClickListener() {
....
#Override
public void onClick(View view) {
Intent goBackToMainActivity = new Intent(this, MainActivity.class);
startActivity(goBackToMainActivity);
finish();
}
});
But remember,this also stops the video from playing if for instance your phone goes into sleep mode, or anything else that interferes with the activity (dialogs etc.).
I advice you take a couple of minutes to read up on the basics of Activity in Android.This is a good place to start.
Alternatively, in the manifest you can set the "noHistory" attribute in your activity element to true.
Read more on that here

Related

Android finishing activity not working

Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.
I have tried using "finish();" after the activity starts but nothing seems to be working.
Activity that holds the EditText that launches the List activity:
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 1);
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
CPU.setText(product);
}
});
List view class
#Override
public void onCreate(Bundle OnsaveInstanceState) {
super.onCreate(OnsaveInstanceState);
setContentView(R.layout.activity_cpulist);
ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish();
}
});
You need to launch your activity in a way that it doesn't get added to back stack.
Here's how you do that: https://stackoverflow.com/a/12358563/375929
If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(),
ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(getIntent());
startActivity(i);
finish(); // finish here
}
and remove finish() from your EditText Activity
Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish(); // finish here
}

How to set Android button inside dialog that was called from listview?

I have code that goes like this:
public class Screening extends ListActivity implements OnClickListener{
...
Button screeningOK, set, cancel;
...
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
int listPosition = position;
switch(listPosition){
case _AGESELECTION:
Dialog d = new Dialog(Screening.this);
d.setTitle(R.string.ageselector_title);
set = (Button)d.findViewById(R.id.bSet);
cancel = (Button)d.findViewById(R.id.bCancel);
//set.setOnClickListener(this);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
d.setContentView(R.layout.agedialog);
d.show();
break;
So short explanation, I have a list that opens dialog.
Dialog has NumberPicker, set and cancel buttons.
Now, on Set, I'd like to save NumPicker value, and cancel to cancel dialog. (finish(); will most likely finish activity but that's not the issue, issue is that no matter if I set new on click listener like:
set.setOnClickListener(this);
or
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
It crashes my app with Null pointer exception.
There is onClick function within main class because original activity consists of list, radio buttons and Save Button.
I assume something with the fact that I already have onClickListener is giving me hard time here, but can't figure out what.
Please Advise.
tnx.

onPause gets called before leaving the current activity

I have the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
populate();
handler = new Handler();
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
doReload1();
populate();
}
});
}
}, 300, 30000);
}
private void populate() {
if (playlists.length != 0) {
MyListView = (ListView) findViewById(R.id.MyListView);
for (String item : playlists) {
Log.d("DEBUG", "item=" + item);
}
String[] adapterPlaylists = new String[playlists.length];
for (int i = 0; i < playlists.length; i++) {
adapterPlaylists[i] = playlists[i];
}
adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, adapterPlaylists);
MyListView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
MyListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v,
int position, long id) {
dialogwait = ProgressDialog.show(Playlist.this,
"Loading...", "Please wait..", true);
Intent i = new Intent(getBaseContext(), ViewPlaylist.class);
i.putExtra("id", idPlaylist[position]);
i.putExtra("timer", timerPlaylist[position]);
startActivity(i);
finish();
}
});
} else
System.out.println("playlist null");
}
#Override
protected void onPause() {
super.onPause();
System.out.println("onPause Playlist!!!!!!");
dialogwait.dismiss();
t.cancel();
}
The thing is that here:
dialogwait = ProgressDialog.show(Playlist.this,
"Loading...", "Please wait..", true);
I create a ProgressDialog and I dissmis it in onPause().
But onPause gets called right after onCreate() before I even the ProgressDialog is created.
Any idea why?ANy solution?Thanks
This is because a Dialog in Android, does not block - meaning that the thread running behind it (in this case your Activity and in particular your onItemClickListener) will continue to execute.
It looks like you want to display a loading dialog, to let the user know that the item he clicked is being loaded. I suggest you move that dialog to the next activity (the one started from onClick), and then display and dismiss the dialog from there.
I think that the problem could be that you are calling finish() in your OnItemClickListener. This will cause the current Activity to end as soon as you click on an item which, because of your onPause() implementation will immediately close the dialog.
Maybe you need to display the dialog when your ViewPlaylist Activity is created rather than in this Activity.
anything on top of your current activity (a dialog or any other activity) makes it a background activity, thus onPause is called. if anything, dialog.dismiss() should be called in activities' onResume. in your implementation, you're showing a dialog when a button is clicked, thus pausing your activity and calling dismiss. you should call dismiss only when the process associated with your dialog is finished, thus telling your user that you're ready to do something else - in your case launch another activity.
launch your activity inside your dialog's onDismiss.
for example:
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// start the other activity
}
});

Android listview

I have created an android application. In that application when I click the listview item it should display in the another listview in the same layout.
Is this possible in android?
Well, quick snippet:
public Activity1 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
// ListView event
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedString", listView.getItemAtPosition(position));
startActivity(intent);
}
});
}
}
public Activity2 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
String valueFromActivity1 = getIntent().getString("SelectedString");
// ok now, u've got value from Activity1, do whatever w/ it
}
}
No you have to make intent and pass the variables of the current selected item of the listview to that intent and display the dynamic listview for that Item

Android loading different contents in the same screen but different activities

i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.

Categories

Resources