Unable to switch between Activities

Put your problem here if it does not fit any of the other categories.

Unable to switch between Activities

Postby tenTypWTK » Tue Jun 01, 2010 11:53 am

Hi, i'm a beginner android developer with some java knowledge. My question is about starting and finishing intents. I know how to start an intent, i know 2 ways (startActivity with 'forResult' and without it). The thing I can't figure out is how to make my sub activity act after pressing back button like going back to previous activity. In my case it always go to home desktop of system. What am I doing wrong? I was trying to analyze Android Api Demos but I still don't see differences between my and their code that makes that difference in action. Any tips, any examples? :-)
tenTypWTK
Freshman
Freshman
 
Posts: 6
Joined: Tue Jun 01, 2010 11:50 am

Top

Re: Unable to switch between Activities

Postby anarche » Tue Jun 01, 2010 12:12 pm

Can you please post code?

Have you reassigned the Back button?
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Unable to switch between Activities

Postby tenTypWTK » Tue Jun 01, 2010 12:59 pm

I can't find in any Api Demo like Note Pad or Jet Boy or any other app/game any reassignation for back button. For new it seems like native function for that button. When i add a theme to new activity, and then it looks like a dialog (in background there is still previous activity) then it acts normal, as it should - it takes me to previous activity.

Main.java
Code: Select all
       ComponentName componentName = new ComponentName("com.example",
          "com.example." + activityName);
       Intent runActivity = new Intent();
       runActivity.setComponent(componentName);
       startActivityForResult(runActivity, 5);

{ i changed it from startActivity to ..forResult, doesn't matter in action so far :( }


Manifest
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name="CameraActivity" android:screenOrientation="landscape">
</activity>
      <activity android:name="Main2" android:label="tescik">
      </activity>
      <uses-sdk android:minSdkVersion="7" />
    </application>
   
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />   
</manifest>


Should I override some methods in sub activities? As I mentioned i don't see such things in examples found on internet... Is it necessary? I thought it should work this way natively...

I experimented with setting result to OK etc.

Subactivity:
Code: Select all
   public void onCreate(Bundle b){
      super.onCreate(b);
      setContentView(R.layout.obrazek);
      ((Button) findViewById(R.id.b1)).setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            Intent resultIntent = new Intent();
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
         }
      });

{ it's not back button, but i tried to make any button to behave as i wanted... }
tenTypWTK
Freshman
Freshman
 
Posts: 6
Joined: Tue Jun 01, 2010 11:50 am

Re: Unable to switch between Activities

Postby nicholas.hauschild » Tue Jun 01, 2010 1:47 pm

Well, this certainly is strange, it seems it should work just fine.

I do question how you start your other activity. I believe this is not the desired way to start a new activity, especially if it is your own activity defined in this project.
tenTypWTK wrote:Main.java
Code: Select all
       ComponentName componentName = new ComponentName("com.example",
          "com.example." + activityName);
       Intent runActivity = new Intent();
       runActivity.setComponent(componentName);
       startActivityForResult(runActivity, 5);
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: Unable to switch between Activities

Postby tenTypWTK » Tue Jun 01, 2010 6:35 pm

...of course in place where you see "com.example" i put my package name. If you could - provide me a simple project with starting new activity, where back button should be working... I hope it's not an SDK/Eclipse issue.
tenTypWTK
Freshman
Freshman
 
Posts: 6
Joined: Tue Jun 01, 2010 11:50 am

Re: Unable to switch between Activities

Postby nicholas.hauschild » Wed Jun 02, 2010 1:45 am

Have you tried something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent(getApplicationContext(), MyActivityToTransitionTo.class);
  2. startActivityForResult(i,5);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Top

Re: Unable to switch between Activities

Postby tenTypWTK » Wed Jun 02, 2010 9:15 am

still the same. what is funny - when i was debugging my app, it once worked fine, showed Toast in main activity after pressing back. I don't know what is wrong - maybe it takes my pressing back as double back? or sth..

As I have already asked previously - if anyone could provide small example which WORKS on his environment... only 2 activities, switching etc.
----------------------------------------
edit:
another funny thing - i created basic project, 2 activities.. and now it switches between activities. I must have added some lines which are not needed :/

----------------
edit2:
Code: Select all
    @Override 
    protected void onStop() {   
         super.onStop(); 
       this.finish(); 
    } 


this what i had in my code. don't ask me why i did that. Oh..... :-) problem solved
tenTypWTK
Freshman
Freshman
 
Posts: 6
Joined: Tue Jun 01, 2010 11:50 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests