• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing unwanted activities from Activity Stack

 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,List of Activities in my contacts app goes as:

Splash Activity>Select Activity>here users can select from three options(add contacts,list contacts,settings)
1.add contacts>insert,edit,delete
2.list contacts>list of details will be populated in a list view.
3.settings>Sounds on/off radio or toggle button.
My problems are:
when a user successfully enters details ,a toast is displayed and activity is directed to list where he can see the latest entered contact.This is fine but
1.when he enters back button the user is redirected to the same edit screen which I don't want that to happen.
2.in settings activity I kept a toggle button through which user can set sound on/off.I am unable to implement this .

Any references ,suggestions,code ideas are appreciated.Thanks in Advance
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when he enters back button the user is redirected to the same edit screen

If the edit was successful you should finish the activity. If the fact of the edit being successful is determined in some other activity, start that other activity with startActivityForResult, so that the edit activity can receive feedback from the other activity.

in settings activity I kept a toggle button through which user can set sound on/off.I am unable to implement this .

What do you have so far, and how is that working or not working according to your expectations? Post relevant code excerpts.
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear ulf sir,
I am doing that startActivityForResult after it is done I will inform you.

coming to my togglebutton issue,here is the code i wrote so far,

if(myToggleButton.isEnabled()){

audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);


}
else{
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);


}

the application is crashing whenever I clicked it.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever an app crashes there'll be a stack trace in the logcat output; that's where you should start the debugging.
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ulf sir,

I found it more comfortable to reduce tracking history of activities in activity stack by adding a line of code in manifest file.I solved it by adding a code

<activity android:noHistory="true">in my manifest file.

Sir,is there any possibility of muting/unmuting app sounds from manifest file?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<activity android:noHistory="true">

For an "edit" activity in particular this may make sense, but you shouldn't do that more generally: users are accustomed to activities still being there.

is there any possibility of muting/unmuting app sounds from manifest file?

The manifest file is meta-data about the application, it doesn't run code. What did your logcat examination find?
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your valuable suggestion sir.I Shall practice this from now on..Coming to my logcat :
it displays java.lang.nullpointerException
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to find out which object is null. And then you need to find out why the code that you think assigns a value to that object reference, instead assigns null. This may involve reading the javadocs of that method (assuming it's part of the Android API).
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir , I solved the problem by doing :

sound_btn=(Button)findViewById(R.id.sounds_on_btn);
mute_btn=(Button)findViewById(R.id.sounds_off_btn);
sound_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);
Toast.makeText(getApplicationContext(), "Sounds on",Toast.LENGTH_SHORT).show();
}

});

mute_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);
Toast.makeText(getApplicationContext(), "Sounds Off",Toast.LENGTH_SHORT).show();
}

});

this is working fine sir,but now I am seeing a different problem.This problem is arising if I turn off the sound and close the app.Next time when I start the app it is not giving any sounds.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it is still muted? Does your code unmute audio on startup?
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added this code

if(!mAudioManager.isMusicActive)
{
mAudioManager.setStreamMute(...,false)
}

in my splashActivity onCreate Method But the application is getting crashed.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Whenever an app crashes there'll be a stack trace in the logcat output; that's where you should start the debugging.
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry sir, the question was incomplete.I lost internet connectivity before i clicked....here is the complete question
it shows nullPointerException,which I will resolve.Sir, now the problem arises in list activity.
ListActivity displays list of items in a list.Like:

1(index autoincrement).Item Name1
2.Item Name2
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6.........so on

Problem:If I delete any item say item 2,
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "the index"? Something in your code? Remember that we have neither the app nor the code in front of us.
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it shows nullPointerException,which I will resolve.Sir, now the problem arises in list activity.
ListActivity displays list of items in a list.Like:

1(index autoincrement).Item Name1
2.Item Name2
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6.........so on

Problem:If I delete any item say item 2,item 3 goes to item 2 position but the index is not getting updated.Index remains same just positions getting changed.

After deleting item 2.The listActivity displaying like this
1.Item Name1
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6......

which is supposed to display as:

1.Item Name1
2.Item Name3
3.Item Name4
4.Item Name5
5.Item Name6......

codes:
DISPLAYCONTACT:
DisplayContact extends activity{

mydb.deleteContact(id_To_Delete);
Toast.makeText(getApplicationContext(), NAME+" 's"+"contact Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),com.dinesh.contactsBook.MainActivity.class);
startActivity(intent);
}


MAINACTIVITY:
MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
mydb = new DBHelper(this);
ArrayList<String> array_list = mydb.getAllContacts();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_list);

obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int id_To_Search = arg2;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.dinesh.contactsBook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
catch (Exception e)
{
e.printStackTrace();

}
}

DBHELPER:
public class DBHelper extends SQLiteOpenHelper
{
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("myTable", "id = ? ", new String[] { Integer.toString(id) });

}
public ArrayList<String> getAllContacts()
{
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from myTable", null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
String Index=res.getString(res.getColumnIndex(CONTACTS_COLUMN_ID));
String Name=res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
array_list.add(Index+"."+Name);
res.moveToNext();
}
db.close();
return array_list;
}
}

PROBLEMS:
1.when item 2 is deleted ,item 3 comes to item 2 position but when it is clicked to see the details logcat displays
cursorIndexOutofBoundsException :Index 0 requested, with a size of 0.With my little knowledge I can understand that the error is arising because i am trying to access which is not present.Dear Ulf,Kindly check the code .
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't ever edit a post after there have been responses to it - it makes the responses look either foolish, wrong or meaningless.

CONTACTS_COLUMN_ID is a DB field - why would that change just because some other record is deleted?
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Please don't ever edit a post after there have been responses to it - it makes the responses look either foolish, wrong or meaningless.

I admit my mistake.I wont do that ..Thanks .
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry sir, i didnt do it deliberately.
It shows nullPointerException,which I will resolve.Sir, now the problem arises in list activity.
ListActivity displays list of items in a list.Like:

1(index autoincrement).Item Name1
2.Item Name2
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6.........so on

Problem:If I delete any item say item 2,item 3 goes to item 2 position but the index is not getting updated.Index remains same just positions getting changed.

After deleting item 2.The listActivity displaying like this
1.Item Name1
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6......

which I wish it to display as:

1.Item Name1
2.Item Name3
3.Item Name4
4.Item Name5
5.Item Name6......

codes:
DISPLAYCONTACT:

MAINACTIVITY:

DBHELPER:

PROBLEMS:
1.when item 2 is deleted ,item 3 comes to item 2 position but when it is clicked to see the details logcat displays
cursorIndexOutofBoundsException :Index 0 requested, with a size of 0.With my little knowledge I can understand that the error is arising because i am trying to access which is not present.Dear Ulf,Kindly check the code .
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which line of code does this happen? What are the values of all involved variables at that point during the execution (not what you *think* they are - *what* they are) ?
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here lies the problem I guess,

res.moveToFirst();
while(res.isAfterLast() == false)
{
String Index=res.getString(res.getColumnIndex(CONTACTS_COLUMN_ID));
String Name=res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
array_list.add(Index+"."+Name);
res.moveToNext();
}
db.close();
return array_list;
}
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you "guess"? The stack trace tells you exactly in which line the exception occurs. Which line is it?
 
dinnu goud
Greenhorn
Posts: 19
Android Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you "guess"? The stack trace tells you exactly in which line the exception occurs. Which line is it?

Yeah I understand.I followed and got my app problems resolved.Thank you for your responses.

Problems I had at the beginning were:
1.sounds on/off button
2.Updating arraylist items.

Solutions I tried:

In soundsActivity a user can turn on/off sounds by clicking on a button.My code for it is:

I wrote the following code

in onCreate() method to turn on the sounds when app is restarted.


2.Updating arraylist problem.
Initially my arraylist will look like :
1.ItemName1
2.ItemName2
3.ItemName3
4.ItemName4
5.ItemName5 ..........when user clicks on any of the items he will be directed to that particular details activity.This worked fine as far as user doesn't delete any item.When an item is deleted from it ,arraylist should look as: (say ItemName2 is deleted)
1.ItemName1
2.ItemName3
3.ItemName4
4.ItemName5
when ItemName3 is clicked application got crashed.Error was curcor indexOutfBounds:index requested 0 with size 0 exception as I mentioned earlier.I tried to solve it this way:

now my application is working fine.earlier the above block of code I tried was:


The application is working fine so far.I am curious to know whether my approach for sounds on/off button is correct.Because I googled about the problem and what I discovered was
people used the following code to mute/unmute:

I tried this in my app. It muted my app sound but when I restarted the app it got the app sound still muted.I was unable to increase the volume either.so I used setStreamVolume().
Appllication is working fine.Kindly see the above codes and suggest any best practices..Thank you Ulf Sir.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic