Android call method from intent - android

I've this code:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, ConnectDialog.class);
update();
}
private void update(){
if(a)
startActivity(intent);
else{
//code
}
}
}
And this:
public class ConnectDialog extends Activity{
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_dialog);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
The problem is: when I click on the button of the Intent, is it possible to execute the method update of the main activity again? Thank you very much

Just put the call to update into onResume() of the MainActivity. This way, it will be called at first startup and when the MainActivity is shown again later on:
#Override
protected void onResume(){
super.onResume;
update();
}

I would use the onActivityResult method.
Change your update method to
int YOUR_RESULT = 100;
private void update(){
if(a)
startActivityForResult(intent, YOUR_RESULT);
else{
//code
}
}
then in that same activity, use this method:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_RESULT) {
update();
}
}
If you use this method, the update() method will only get called when coming back from that activity.

if u check the Android Life Cycle u will get the answer why the Update is not executing..
How ever to call the update method on click..
private void update(){
if(a)
startActivityForResult(intent, 0);
else{
//code
}
}
and use onActivityResult to call the update method again
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
update();
}

Related

How to send data by click back button?

I'm using two activities, MainActivity & SubActivity.
MainActivity.class
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivity(sub);
}
}
}
}
SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
In this case, if I change the text in EditText in SubActivity, I want to send changed text data to MainActivity when back button pressed in SubActivity(It means SubActivity onDestroy()). I don't want to make the second MainActivity like this in SubActivity.
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
startActivity(intent);
}
Is there any way to send text data when SubActivity removed from the activity stack?
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivityForResult(sub , 2);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("name");
textView1.setText(message);
}
}
}
And
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
setResult(2,intent);
}
You need to use startActivityForResult() if you expect to have a return from the started activity.
After that, you'll also need to implement onActivityResult() method on the start activity to actually read the returned data.
Use startActivityForResult instead of startActivity
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
String name= "a";
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", name);
startActivityForResult(sub,100);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 100) {
String name = data.getStringExtra("name);
text.setText(name);
}
}
}
}
SubActivity:
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
Intent resultIntent = new Intent();
resultIntent.putExtra("name", "your_editext_value");
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can use SharedPreference for storing value and update it or use it anywhere instead of using Intent
like In MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Editor edit=sp.edit();
edit.putString("name","a");
edit.commit();
Intent sub = new Intent(this, SubActivity.class);
startActivity(sub);
}
}
} }
and In SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(sp.getString("name",""));
text.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editor edit=sp.edit();
edit.putString("name",s.toString);
edit.commit();
} });
}
#Override
public void onBackPressed() {
super.onBackPressed();
}}
To use value from SharedPreference
sp.getString("name","");
For using SharedPreference look at this https://developer.android.com/training/data-storage/shared-preferences#java

startActivityForResult() working after I click it again?

Guys I have 2 activity when I clicked button in main starting new activity(message.java) and I'm trying to get data from my message activity
to mainActivity .In message activity, I have edittext and when clicked button go back mainactivity and show that info in textview. This code working but problem is when I clicked button in mainActivity
it's first delete textview's text and then message activity begining . This is not what I want and also , it's taking info from message activity that's OK but in mainActivity I must clicked again mainActivtiy button to show in textview. Any suggest?
This is what I want
This picture shows problem
Parent -> MainActivity
Child ->message
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
tv.setText(s);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
}
}
}
}//end of mainActivity
here message.java
public class MessageActivity extends Activity {
private Button bt;
private TextView tv;
private EditText et;
private String s;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
bt=(Button)findViewById(R.id.button);
et=(EditText)findViewById(R.id.editText);
tv=(TextView)findViewById(R.id.textView);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
s=et.getText().toString();
data.putExtra("info", s);
setResult(RESULT_OK, data);
finish();
}
});
}
}
Im not sure if I understood your question correctly, but if you would like to show your text in MainActivity when you come back from MessageActivity, you should just be setting the text in onActivityResult() method, not in the clickListener.
So your code will look like this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
Why don't you try to set the textView text in the activity result function. Is the better place to do it. Think than when you start a child Activity, the flow of parent activity stops. And when you finish child activity, flow goes directly to onActivityResult function. Try it. Regards.
In your MainActivity you need to set the text on your textview once you receive it. Problem is that startActivityForResult is asynchronous call. Once call to this method is gone it will start other activity but your onClick() method will not stop here. Its a normal listener that calls onActivityResult when your activity is destroyed.
public class MainActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private Button bt,bt2;
private TextView tv;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
tv=(TextView)findViewById(R.id.textView2);
bt2=(Button)findViewById(R.id.button4);
final Intent i=new Intent(getBaseContext(),MessageActivity.class);
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(i, 1);
}
});
}//end of onCreate
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
s=data.getStringExtra("info");
tv.setText(s);
}
}
}
}
Accept and upvote if it works.

startactivityforresult() inside a listener not calling onActivityResult()

I know there are many similar questions on SO, but none of them resolved my issue.
I am calling startActivityForResult inside a seekbar listener. However, onActivityResult is not getting called. Here is my code. What could be going wrong?
public class PeopleActivity extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Intent in = new Intent(getActivity(), com.mytest.mytestninja.RangingActivity.class);
getActivity().startActivityForResult(in, 1);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
rangeValue.setText("CALLED");
}
}
RangingActivity.java
public class RangingActivity extends Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
Intent returnIntent = new Intent();
returnIntent.putExtra("result", firstBeacon.toString());
setResult(RESULT_OK, returnIntent);
finish();
}
}
UPDATE
Activity in which PeopleActivity fragment is used :
public class DrawerFrontActivity extends SherlockFragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bar = getSupportActionBar();
Drawable myIcon = getResources().getDrawable( R.drawable.bluebar);
bar.setBackgroundDrawable(myIcon);
SherlockFragment fragment;
fragment = new PeopleActivity();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.rlDrawerHome, fragment);
transaction.commit();
setTitle("Home");
bar.setTitle("Home");
}
}
It seems you're calling getActivity().startActivityForResult(in, 1); which would pass result back to activity and not fragment it self.
Try using PeopleActivity.this.startActivityForResult(in, 1);, this should pass result back to fragment.
Refer to this answer for more details:
https://stackoverflow.com/a/6147919/2146871
Found the answer.
The issue was because I was not calling super.onactivityresult in my fragment activity DrawerFrontActivity. Hence

How to implement startActivityForResult

In my app I create two activities and I want to get input from the second activity and use it in the first activity, I use startActivityForResult but there is a problem in it!
That is the code of first activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent("net.naif.action.GETDATA");
startActivityForResult(i,77);
}
});
}
protected void onActivityForResult (int requestCode,int resultCode,Intent data){
if (requestCode ==77 && resultCode ==RESULT_OK){
String msg =data.getStringExtra("text");
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
}
}
And this for the second:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Button b=(Button)findViewById(R.id.button);
final EditText t=(EditText)findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s=t.getText().toString();
Intent i=new Intent();
i.putExtra("text",s);
setResult(Activity.RESULT_OK,i);
finish();
}
});
}
Android studio notify me that is the method ( protected void onActivityForResult (int requestCode, int resultCode, Intent data) is never used.
That's because the method is named onActivityResult, not onActivityForResult.

Android code to add and view data through application

I am developing a Task manager( a simple one) to learn android and this is my short description.
I am making two activities one to view task and other to add ( and I am using an Application class through which i add the task)..
when i add the task on the addtask activity my viewtask activity still remains blank. Could you help me out.
This is my viewTask activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
setupviews();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println(requestCode);
if (requestCode == 1) {
showtask(); // your "refresh" code
}
}
public void OnResume()
{
super.onResume();
System.out.println("Its working...");
showtask();
}
/*public void onPause(){
super.onPause();
}*/
public void showtask()
{
ArrayList<Task> task=gettma().gettask();
StringBuffer sb=new StringBuffer();
for(Task t:task){
sb.append(String.format("* %s\n", t.toString()));
System.out.println("Its working...again");
}
taskText.setText(sb.toString());
}
private TaskManagerApplication gettma() {
TaskManagerApplication tma1=(TaskManagerApplication)getApplication();
return tma1;
}
private void setupviews() {
AddButton=(Button)findViewById(R.id.AddButton);
taskText=(TextView)findViewById(R.id.TextList);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(ViewTask.this,AddTask.class);
startActivity(intent);
}
});
}
}
And this is my Addtask activity.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask);
setupviews();
}
protected void addtask() {
String name=EditText.getText().toString();
Task t= new Task(name);
//System.out.println("its working here");
gettma().addTask(t);
finish();
}
private TaskManagerApplication gettma() {
TaskManagerApplication tma=(TaskManagerApplication)getApplication();
return tma;
}
private void setupviews() {
EditText=(EditText)findViewById(R.id.EditText);
AddButton=(Button)findViewById(R.id.AddButton1);
CancelButton=(Button)findViewById(R.id.CancelButton);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtask();
}
});
CancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Please Help
EditText=(EditText)findViewById(R.id.EditText);
This is the line in your setupviews(). You cannot do this because EditText is a class and you need a variable of that type to which you can assign a value. It must be EditText followed by some variable name.
if (requestCode == 1) {
showtask(); // your "refresh" code
}
This is in your onActivityResult(). You aren't checking the resultCode to check if the called Activity failed to do the operation that would produce a result.
Also, in numerous places you arent following the Java naming convention. Please, use upper and lower cases properly when naming variables and classes.

Categories

Resources