I wish that when I click on the apply button firstapp, opens secondapp thus the data passed the press: What did I do wrong?
Secondapp is called but it print null.
App1
package pkg.firstapp;
public class MainActivity extends Activity{
private static final int REQUEST_CODE_START_APP = 12345;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PackageManager manager = getApplicationContext().getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage("pkg.secondapp");
if (i != null)
{
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.putExtra("key1", "value1");
i.setFlags(0);
setResult(RESULT_OK, i);
startActivityForResult(i, REQUEST_CODE_START_APP);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
If you want to get String from Intent, you should call getIntent().getStringExtra("key1"); in onCreate. onActivityResult is called in first Activity after finishing second. Read this for more info
onActivityResult is called in the activity which started other activity for result.
In order to get the data you're sending, in the second activity's onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String value1 = getIntent().getStringExtra("key1");
}
Related
In a project there will be lots of pages(activities) and user will be able to switch between these pages (activities). So when user press the corresponding button that opens page_2 from page_1, I need to create a new Activity. However, if user comes back to page_1 and try to open the page_2 again, there will be a new Activity created again, instead of opening the previously created activity ( I want user to see the page_2 as he/she left it without anychanges). So I want to put something like
if(SecondActivity==null)
{
//Create new activity
}
start(new_activity);
Here is the corresponding code ( I couldn't implement onClickListener because I couldn't disable it in onPause() method... so I used onClick from xml)
public class MainActivity extends Activity {
private View.OnClickListener openSecondPage = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setText("Clicked");
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
//Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(openSecondPage);
}
public void onPause(){
super.onPause();
//Destroy the on click listener
Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(null);
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
}
Edit: Here is the new code with Flags:
MainActivity.java code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
}
public void onPause(){
super.onPause();
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
secondPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(secondPage);
}
}
SecondActivity.java code:
public class SecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
}
protected void onResume()
{
super.onResume();
}
public void goBack(View v)
{
}
public void goMainPage(View v)
{
Intent mainPage = new Intent(getApplicationContext(),MainActivity.class);
mainPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(mainPage);
}
}
I think you can resolve problem by FLAG_ACTIVITY_REORDER_TO_FRONT flag.
You need to set flag when start activity.
public void openSecondPage(View v) {
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class);
secondPage.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(secondPage);
}
You can find more detailed information in this link.
https://developer.android.com/guide/components/activities/tasks-and-back-stack
Show Activity A, then when you need it then show Activity B, then when you need it then show Activity A again, by calling startActivity + setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) => Activity A will show up as it was exactly before showing Activity B
According to your question asked, it seems you have to use either Room or SharedPreference in SecondActivity to save the user input data.
So, whenever you come back to SecondActivity, check if there is already data available. If there is a data available, show it to the user else set the fields with default values.
I want to make a button in an activity disabled if it is intented from a certain button and otherwise enabled if and it is intented from some other class.What I am saying will be clear after reading the code.
This is the function to intent the class containing the button.
showperson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
falses=true;
ob.truth=false;
Toast.makeText(home_page.this, "doneit"+ob.truth, Toast.LENGTH_SHORT).show();
// Button clik=fb.getButton();
// clik.setVisibility(View.GONE);
Intent to=new Intent(home_page.this,Form.class);
to.putExtra("buttonclik",false);
startActivity(to);
}
});
Try to add this in onCreate():
Intent intent = getIntent();
if (intent != null) {
boolean isButtonClicked = intent.getBooleanExtra("buttonclik", false);
if (isButtonClicked) {
showperson.setEnabled(false);
}
}
And change false to true at to.putExtra("buttonclik",false);
You can achieve that by setting a flag from the caller activity. Refer to the example below.
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Intent intent = new Intent(this, ActivityThatContainsTheButton.class);
intent.putExtra("activity", "activityA");
startActivity(intent);
}
}
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = new Intent(this, ActivityThatContainsTheButton.class);
intent.putExtra("activity", "activityB");
startActivity(intent);
}
}
public class ActivityThatContainsTheButton extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x);
Button button = findViewById(R.id.button);
String from = getIntent().getStringExtra("activity");
if (from != null) {
if (from.equals("activityA")) {
button.setVisibility(View.VISIBLE);
} else if (from.equals("activityB")) {
button.setVisibility(View.INVISIBLE);
}
}
}
}
i have following problem:
I've made a simple android app that adds 1 to an integer every 1000 ms using a handler, and then display this integer.
The problem is that when i start another activity the same thing happens, which would be fine, if that was intended. The mentioned function is not called in the new activity and yet it seems to be. Please look over my code and show me where it went wrong..
MainActivity:
public class MainActivity extends ActionBarActivity {
protected TextView text;
protected int position;
private Handler handler = new Handler();
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
position=0;
SetButtonCLickListener();
counter();
}
protected void SetButtonCLickListener() {
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SwitchActivity();
}
});
}
private void counter() {
handler.removeCallbacks(count);
handler.postDelayed(count, 1000);
}
private Runnable count = new Runnable() {
public void run() {
i++;
text.setText("Count: " + i);
handler.postDelayed(count, 1000);
}
};
protected void SwitchActivity() {
if (position == 1) {
finish();
} else {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
}
SecondActivity
public class MainActivity2 extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
text = (TextView) findViewById(R.id.text);
SetButtonCLickListener();
position=1;
}
}
MainActivity2 has an onCreate() method. In the onCreate() method, you call super.onCreate(), which triggers the MainActivity implementation of onCreate(). The MainActivity implementation of onCreate() is where you are starting your counter thing, via its call to the counter() method. Hence, when MainActivity2 starts up, its onCreate() calls MainActivity's onCreate(), which calls counter().
My guess is that MainActivity2 should inherit from Activity, not from MainActivity.
I have an Android App That requires a button to open up a new xml page. This is what it's like now, could someone add the necessary code to make it open Page2Activity when I click on the button? Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Void onClick;View arg0; {
// TODO Auto-generated method stub
}
};
}
I figured this out using this method : http://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,but I will try All of yours as well.
Try this code:
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,Page2Activity.class);
startActivity(intent);
}
Then create a new cLass called Page2Activity.
Hope this helps and don't forget to add your activity to the manifest file.
i think you mean something like this:
public class MyClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("com.myaction");
startActivity(i);
}
});
}
}
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying to do call new intents, but my app is always unexpectedly stopped. Do am I correctly call new intents?
First class:
public class first extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button SignIn = (Button) findViewById(R.id.SignIn);
SignIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText pw = (EditText) findViewById(R.id.editPasswd);
if(pw.getText().toString().equals("123")) {
Intent intent = new Intent(first.this, second.class);
startActivity(intent);
}
else {
Toast.makeText(getBaseContext(), "Wrong PIN" ,
Toast.LENGTH_LONG).show();
}
}});
}
}
Second class:
public class second extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginas);
Button Button01 = (Button) findViewById(R.id.Button01);
Button01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(v.getId() == R.id.Button01) {
Intent intent = new Intent(second.this, third.class);
startActivity(intent);
}}
});
}
}
Third class:
public class third extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock);
}}
When I put correct pin code the app show messages "the application () has stopped unexpectedly. Please try again". First I want to know that my code is correct.
whatever the activity u want to display must and mension those activities in AndroidManifest.xml file.For example in ur application first,second,third activities mention in AndroidManifest.xml file